DotNetCoreOutputToConsole 1.0.0

dotnet add package DotNetCoreOutputToConsole --version 1.0.0
                    
NuGet\Install-Package DotNetCoreOutputToConsole -Version 1.0.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="DotNetCoreOutputToConsole" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetCoreOutputToConsole" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DotNetCoreOutputToConsole" />
                    
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 DotNetCoreOutputToConsole --version 1.0.0
                    
#r "nuget: DotNetCoreOutputToConsole, 1.0.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 DotNetCoreOutputToConsole@1.0.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=DotNetCoreOutputToConsole&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DotNetCoreOutputToConsole&version=1.0.0
                    
Install as a Cake Tool

DotNetCoreOutputToConsole

A lightweight ASP.NET Core helper library that allows developers to output logs, variables, and errors directly into the browser console using inline <script> injection.

Ideal for Development, Debug, and UAT environments.


๐Ÿ“ฅ Download

You can download the full source code from your GitHub repository once uploaded:

https://github.com/<your-account>/DotNetCoreOutputToConsole

If you need a GitHub release ZIP template, just tell me.


โœจ Features

  • Write messages directly to browser DevTools console
  • Supports multiple console API functions:
    • console.log
    • console.info
    • console.warn
    • console.error
    • console.trace
    • console.table
  • Automatically JSON-encodes objects (safe for XSS)
  • Easy integration with ASP.NET Core
  • Only runs in:
    • Development
    • Staging
    • UAT
  • Works with:
    • Minimal API
    • MVC
    • Razor Pages

โ— Not Supported

This library does NOT support Blazor Web App, Blazor Server, or Blazor WebAssembly.

Blazor does NOT execute inline <script> inside its rendering pipeline.
To support Blazor, a JSInterop-based version would be required (future extension).


๐Ÿ“ฆ Installation

Add the library project reference:

<ProjectReference Include="..\DotNetCoreOutputToConsole\DotNetCoreOutputToConsole.csproj" />

Or include the source project directly in your solution.


โš™ Environment Setup

Set the environment name so console output only appears in safe environments:

ConsoleOutputSettings.CurrentEnvironment = builder.Environment.EnvironmentName;

The library will ONLY output console messages when the environment is:

  • Development
  • Staging
  • UAT

In Production, all calls become no-ops (silently ignored).


๐Ÿš€ Usage Examples

โญ Minimal API Example

app.MapGet("/test", async ctx =>
{
    await BrowserConsole.Log(ctx.Response, "Hello from Minimal API");
    await BrowserConsole.Info(ctx.Response, new { User = "livecode", Time = DateTime.UtcNow });
    await BrowserConsole.Warn(ctx.Response, "This is a warning");
    await BrowserConsole.Error(ctx.Response, "This is an error");
    await BrowserConsole.Trace(ctx.Response, "Trace message");
    await BrowserConsole.Table(ctx.Response, new[] {
        new { Id = 1, Name = "Item A" },
        new { Id = 2, Name = "Item B" }
    });

    await ctx.Response.WriteAsync("<h3>Console test complete โ€“ open DevTools.</h3>");
});

โญ MVC Example

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        BrowserConsole.Log(Response, "MVC HomeController loaded");
        BrowserConsole.Info(Response, new { Page = "Home/Index", Time = DateTime.UtcNow });
        BrowserConsole.Warn(Response, "This is a test warning from MVC");
        BrowserConsole.Error(Response, new { Error = "Something failed", Code = 500 });

        return View();
    }
}

View (Views/Home/Index.cshtml)

<h2>MVC Console Test Page</h2>
<p>Check your browser console.</p>

โญ Razor Pages Example

Pages/Index.cshtml.cs

public class IndexModel : PageModel
{
    public void OnGet()
    {
        BrowserConsole.Info(Response, "Razor Page Loaded");
        BrowserConsole.Table(Response, new[] {
            new { Id = 1, Description = "Razor Row 1" },
            new { Id = 2, Description = "Razor Row 2" }
        });
    }
}

Pages/Index.cshtml

@page
@model IndexModel

<h3>Razor Pages Console Test</h3>
<p>Open browser DevTools โ†’ Console.</p>

๐Ÿงฉ Console API Supported

Function Method in Library Example Usage
console.log BrowserConsole.Log() await BrowserConsole.Log(Response, "msg");
console.info BrowserConsole.Info() await BrowserConsole.Info(Response, obj);
console.warn BrowserConsole.Warn() await BrowserConsole.Warn(Response, "warn");
console.error BrowserConsole.Error() await BrowserConsole.Error(Response, ex);
console.trace BrowserConsole.Trace() await BrowserConsole.Trace(Response, "trace");
console.table BrowserConsole.Table() await BrowserConsole.Table(Response, list);

All methods serialize objects to JSON and safely embed them into <script> blocks, which are then executed by the browser.


๐Ÿ” Security Notes

  • Objects are JSON-encoded before being written into the script.
  • Output is restricted to non-production environments via ConsoleOutputSettings.
  • If writing to the response fails (stream closed, headers sent, etc.), the library will silently ignore the error so it never breaks your application behavior.

๐Ÿ“ Project Structure

A typical solution layout:

DotNetCoreOutputToConsole/
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ DotNetCoreOutputToConsole/
โ”‚   โ”‚   โ”œโ”€โ”€ DotNetCoreOutputToConsole.csproj
โ”‚   โ”‚   โ”œโ”€โ”€ BrowserConsole.cs
โ”‚   โ”‚   โ””โ”€โ”€ Config/
โ”‚   โ”‚       โ””โ”€โ”€ ConsoleOutputSettings.cs
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ DotNetCoreOutputToConsole.Tests/
โ”‚   โ”‚   โ”œโ”€โ”€ DotNetCoreOutputToConsole.Tests.csproj
โ”‚   โ”‚   โ”œโ”€โ”€ BrowserConsoleTests.cs
โ”‚   โ”‚   โ””โ”€โ”€ Config/
โ”‚   โ”‚       โ””โ”€โ”€ ConsoleOutputSettingsTests.cs
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ MinimalApiConsoleTestApp/
โ”‚   โ”‚   โ”œโ”€โ”€ MinimalApiConsoleTestApp.csproj
โ”‚   โ”‚   โ””โ”€โ”€ Program.cs
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ MvcConsoleTestApp/
โ”‚   โ”‚   โ”œโ”€โ”€ MvcConsoleTestApp.csproj
โ”‚   โ”‚   โ”œโ”€โ”€ Program.cs
โ”‚   โ”‚   โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ HomeController.cs
โ”‚   โ”‚   โ””โ”€โ”€ Views/
โ”‚   โ”‚       โ””โ”€โ”€ Home/
โ”‚   โ”‚           โ””โ”€โ”€ Index.cshtml
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ RazorPagesConsoleTestApp/
โ”‚       โ”œโ”€โ”€ RazorPagesConsoleTestApp.csproj
โ”‚       โ”œโ”€โ”€ Program.cs
โ”‚       โ””โ”€โ”€ Pages/
โ”‚           โ”œโ”€โ”€ Index.cshtml
โ”‚           โ””โ”€โ”€ Index.cshtml.cs
โ”‚
โ””โ”€โ”€ README.md

๐Ÿงช Unit Tests

Unit tests (in DotNetCoreOutputToConsole.Tests) may include:

  • Verifying console script injection content contains the correct function (log/info/warn/error/trace/table).
  • Ensuring no content is written when environment is Production.
  • Verifying that different message types (string, object, collections) serialize correctly to JSON.

Example snippet:

[Fact]
public async Task Log_WritesConsoleLogScriptInDev()
{
    ConsoleOutputSettings.CurrentEnvironment = "Development";

    var ctx = new DefaultHttpContext();
    ctx.Response.Body = new MemoryStream();

    await BrowserConsole.Log(ctx.Response, "Test message");

    ctx.Response.Body.Seek(0, SeekOrigin.Begin);
    var html = new StreamReader(ctx.Response.Body).ReadToEnd();

    Assert.Contains("console.log", html);
    Assert.Contains("Test message", html);
}

๐Ÿ‘ค Author

livecode


๐Ÿ“ License

MIT License โ€” see LICENSE.


โญ Support This Project

If this library makes your ASP.NET Core debugging easier, consider giving it a โญ on GitHub!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.0 195 11/15/2025

Initial release of DotNetCoreOutputToConsole.