A secure ASP.NET Framework (4.8 / 4.8.1) helper library that writes logs, variables, and exceptions directly to the browser console — perfect for UAT and DEV environments.
- Write messages, variables, and errors to the browser console (
console.info,console.log,console.error) - Automatically logs unhandled exceptions globally
- Works with any ASP.NET Web Forms or MVC project
- Simple global toggle via
web.config - XSS-safe using
System.Web.Helpers.Json.Encodeto sanitize all outputs - Zero third-party dependencies
- Includes unit tests and a demo web app
- Fully compatible with .NET Framework 4.8 to 4.8.1
- Removed dependency on
System.Web.Helpers(Json.Encode()no longer required) - Now uses HttpUtility.JavaScriptStringEncode (safe, built-in, no external packages)
- Improved XSS safety logic
- Cleaned references to reduce DLL size
- Updated module to correctly handle missing AppSettings
- Assembly version sync:
1.1.0.0 - NuGet package version:
1.1.0
- Write messages, variables, and errors to the browser console (
console.info,console.log,console.error) - Automatically logs unhandled exceptions globally
- Works with any ASP.NET Web Forms or MVC project
- Simple global toggle via web.config
- No third-party dependencies
- Includes unit tests and demo web app
- Fully compatible with .NET Framework 4.8 to 4.8.1
Install from NuGet using the Package Manager Console:
Install-Package DotNetOutputToConsoleWhen deploying ASP.NET applications to UAT or DEV environments, developers often need to inspect runtime values or exceptions directly in the browser console. This library provides a safe, fast, invisible way to do that without altering page UI or showing alert popups.
Example:
DotNetOutputToConsoleLogger.LogVariable("SessionId", Session.SessionID);
DotNetOutputToConsoleLogger.LogError("Missing input data");Console output:
LOG: SessionId: 1a2b3c4d
ERROR: Missing input data
| Component | Description |
|---|---|
DotNetOutputToConsoleLogger |
Core class that writes sanitized console commands into browser output. |
DotNetOutputToConsoleHttpModule |
Automatically logs unhandled exceptions at the application level. |
web.config switch |
Allows turning console output on or off globally. |
Json.Encode() |
Sanitizes all messages to avoid XSS or script injection. |
Add the following to your Web.config:
<configuration>
<appSettings>
<add key="EnableOutputToConsole" value="true" />
</appSettings>
<system.webServer>
<modules>
<add name="DotNetOutputToConsoleHttpModule"
type="DotNetOutputToConsole.DotNetOutputToConsoleHttpModule" />
</modules>
</system.webServer>
</configuration>Set "EnableOutputToConsole" to "true" in UAT or DEV.
Set to "false" in production.
DotNetOutputToConsoleLogger.LogInfo("Page load completed");Output:
INFO: Page load completed
DotNetOutputToConsoleLogger.LogVariable("Username", user.Name);Output:
LOG: Username: JohnDoe
try
{
throw new Exception("Simulated failure");
}
catch (Exception ex)
{
DotNetOutputToConsoleLogger.LogError(ex.Message);
}Output:
ERROR: Simulated failure
protected void Page_Load(object sender, EventArgs e)
{
throw new Exception("Unhandled page error!");
}Output:
ERROR: Unhandled Exception: Unhandled page error!
public static class DotNetOutputToConsoleLogger
{
public static void LogInfo(string message);
public static void LogVariable(string name, object value);
public static void LogError(string message);
}public class DotNetOutputToConsoleHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.Error += (sender, e) =>
{
var ex = HttpContext.Current?.Server.GetLastError();
if (ex != null)
DotNetOutputToConsoleLogger.LogError($"Unhandled Exception: {ex.Message}");
};
}
}| Feature | Description |
|---|---|
| XSS Protection | All messages pass through Json.Encode() to escape special characters. |
| Safe Output | Only writes inside valid <script> blocks. |
| Config Toggle | Can be disabled instantly using Web.config. |
| Recommended Usage | Enable only in UAT/DEV. |
Install-Package NUnit
Install-Package NUnit3TestAdapter
Install-Package Microsoft.NET.Test.Sdk[Test]
public void LogInfo_ShouldNotThrow()
{
Assert.DoesNotThrow(() => DotNetOutputToConsoleLogger.LogInfo("info"));
}Visual Studio 2022 → Test Explorer → Run All Tests
or CLI:
dotnet testDotNetOutputToConsole/
├── src/
│ └── DotNetOutputToConsole/
│ ├── DotNetOutputToConsoleLogger.cs
│ ├── DotNetOutputToConsoleHttpModule.cs
│ ├── Properties/
│ │ └── AssemblyInfo.cs
│ ├── README.md
│ └── LICENSE
├── demo/
│ └── DotNetOutputToConsole.DemoWeb/
│ ├── Default.aspx
│ ├── Default.aspx.cs
│ └── Web.config
└── tests/
└── DotNetOutputToConsole.Tests/
└── DotNetOutputToConsoleLoggerTests.cs
- The logger injects
<script>tags into the HTTP response. - Messages are encoded using
Json.Encode()to prevent script injection. - Output executes inside the browser console.
- View results using Developer Tools → Console.
Created by: livedcode
GitHub: https://github.com/livedcode
NuGet: https://www.nuget.org/profiles/livedcode
MIT License © 2025 livedcode