DotNetOutputToConsole 1.1.0

dotnet add package DotNetOutputToConsole --version 1.1.0
                    
NuGet\Install-Package DotNetOutputToConsole -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetOutputToConsole" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetOutputToConsole" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DotNetOutputToConsole" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetOutputToConsole --version 1.1.0
                    
#r "nuget: DotNetOutputToConsole, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DotNetOutputToConsole@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetOutputToConsole&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DotNetOutputToConsole&version=1.1.0
                    
Install as a Cake Tool

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

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.

Configuration

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.

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
{
    throw new Exception("Simulated failure");
}
catch (Exception ex)
{
    DotNetOutputToConsoleLogger.LogError(ex.Message);
}

Output:

ERROR: Simulated failure

Automatic Error Logging (Unhandled Exceptions)

protected void Page_Load(object sender, EventArgs e)
{
    throw new Exception("Unhandled page error!");
}

Output:

ERROR: Unhandled Exception: Unhandled page error!

Class Overview

DotNetOutputToConsoleLogger.cs

public static class DotNetOutputToConsoleLogger
{
    public static void LogInfo(string message);
    public static void LogVariable(string name, object value);
    public static void LogError(string message);
}

DotNetOutputToConsoleHttpModule.cs

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}");
        };
    }
}

Security

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.

Unit Testing

Test Framework Installation

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

Example Test

[Test]
public void LogInfo_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

Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • .NETFramework 4.8.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 315 11/21/2025
1.0.0 240 11/14/2025