Skip to content

Repository files navigation

DotNetOutputToConsole

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.

What's New in v1.0.0

Features

  • 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.Encode to 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

What's New in v1.1.0

  • 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

Features

  • 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

Installation

Install from NuGet using the Package Manager Console:

Install-Package DotNetOutputToConsole

Purpose

When 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

Architecture Overview

ComponentDescription
DotNetOutputToConsoleLoggerCore class that writes sanitized console commands into browser output.
DotNetOutputToConsoleHttpModuleAutomatically logs unhandled exceptions at the application level.
web.config switchAllows turning console output on or off globally.
Json.Encode()Sanitizes all messages to avoid XSS or script injection.

Configuration

Add the following to your Web.config:

<configuration>
<appSettings>
<addkey="EnableOutputToConsole"value="true" />
</appSettings>
<system.webServer>
<modules>
<addname="DotNetOutputToConsoleHttpModule"type="DotNetOutputToConsole.DotNetOutputToConsoleHttpModule" />
</modules>
</system.webServer>
</configuration>

Set "EnableOutputToConsole" to "true" in UAT or DEV. Set to "false" in production.

Usage Examples

Log Information

DotNetOutputToConsoleLogger.LogInfo("Page load completed");

Output:

INFO: Page load completed

Log Variables

DotNetOutputToConsoleLogger.LogVariable("Username",user.Name);

Output:

LOG: Username: JohnDoe

Log Exceptions

try{thrownewException("Simulated failure");}catch(Exceptionex){DotNetOutputToConsoleLogger.LogError(ex.Message);}

Output:

ERROR: Simulated failure

Automatic Error Logging (Unhandled Exceptions)

protectedvoidPage_Load(objectsender,EventArgse){thrownewException("Unhandled page error!");}

Output:

ERROR: Unhandled Exception: Unhandled page error!

Class Overview

DotNetOutputToConsoleLogger.cs

publicstaticclassDotNetOutputToConsoleLogger{publicstaticvoidLogInfo(stringmessage);publicstaticvoidLogVariable(stringname,objectvalue);publicstaticvoidLogError(stringmessage);}

DotNetOutputToConsoleHttpModule.cs

publicclassDotNetOutputToConsoleHttpModule:IHttpModule{publicvoidInit(HttpApplicationcontext){context.Error+=(sender,e)=>{varex=HttpContext.Current?.Server.GetLastError();if(ex!=null)DotNetOutputToConsoleLogger.LogError($"Unhandled Exception: {ex.Message}");};}}

Security

FeatureDescription
XSS ProtectionAll messages pass through Json.Encode() to escape special characters.
Safe OutputOnly writes inside valid <script> blocks.
Config ToggleCan be disabled instantly using Web.config.
Recommended UsageEnable only in UAT/DEV.

Unit Testing

Test Framework Installation

Install-Package NUnit
Install-Package NUnit3TestAdapter
Install-Package Microsoft.NET.Test.Sdk

Example Test

[Test]publicvoidLogInfo_ShouldNotThrow(){Assert.DoesNotThrow(()=>DotNetOutputToConsoleLogger.LogInfo("info"));}

Run Tests

Visual Studio 2022 → Test Explorer → Run All Tests
or CLI:

dotnet test

Project Structure

DotNetOutputToConsole/
├── 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

How It Works

  1. The logger injects <script> tags into the HTTP response.
  2. Messages are encoded using Json.Encode() to prevent script injection.
  3. Output executes inside the browser console.
  4. View results using Developer Tools → Console.

Author

Created by: livedcode
GitHub: https://github.com/livedcode
NuGet: https://www.nuget.org/profiles/livedcode

License

MIT License © 2025 livedcode

About

DotNetOutputToConsole is a lightweight ASP.NET Framework (4.8+) library that securely writes log messages, variable values, and unhandled errors directly to the browser console for easy debugging in UAT or DEV environments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages