11# DotNetOutputToConsole
22
3- > 🧩 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.
3+ 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.
44
5- ---
5+ ## Features
6+ - Write messages, variables, and errors to the browser console (` console.info ` , ` console.log ` , ` console.error ` )
7+ - Automatically logs unhandled exceptions globally
8+ - Works with any ASP.NET Web Forms or MVC project
9+ - Simple global toggle via ` web.config `
10+ - XSS-safe using ` System.Web.Helpers.Json.Encode ` to sanitize all outputs
11+ - Zero third-party dependencies
12+ - Includes unit tests and a demo web app
13+ - Fully compatible with .NET Framework 4.8 to 4.8.1
614
7- ## ✨ Features
8- - ✅ Write messages, variables, and errors to the browser console (` console.info ` , ` console.log ` , ` console.error ` )
9- - ✅ Automatically logs ** unhandled exceptions** globally
10- - ✅ Works with any ** ASP.NET Web Forms** or ** MVC** project
11- - ✅ Simple global toggle via ` web.config `
12- - ✅ ** XSS-safe** — uses ` System.Web.Helpers.Json.Encode ` to sanitize all outputs
13- - ✅ Zero third-party dependencies
14- - ✅ Includes ** unit tests** and a ** demo web app**
15- - ✅ Fully compatible with ** .NET Framework 4.8 → 4.8.1**
16-
17- ---
18-
19- ## ⚙️ Installation
20- Install from NuGet using the .NET CLI or Visual Studio Package Manager Console:
15+ ## Installation
16+ Install from NuGet using the Package Manager Console:
2117
2218``` powershell
2319Install-Package DotNetOutputToConsole
2420```
2521
26- ---
27-
28- ## 💡 Purpose
29- When deploying ASP.NET apps to UAT or DEV servers, developers often need to see ** runtime values** or ** exceptions** directly in the browser console.
30- This library provides a ** safe, fast, and zero-config** way to do that, without changing your page layout or using intrusive alert popups.
22+ ## Purpose
23+ When deploying ASP.NET applications to UAT or DEV environments, developers often need to inspect runtime values or exceptions directly in the browser console.
24+ This library provides a safe, fast, invisible way to do that without altering page UI or showing alert popups.
3125
3226Example:
27+
3328``` csharp
3429DotNetOutputToConsoleLogger .LogVariable (" SessionId" , Session .SessionID );
3530DotNetOutputToConsoleLogger .LogError (" Missing input data" );
3631```
3732
3833Console output:
34+
3935```
4036LOG: SessionId: 1a2b3c4d
4137ERROR: Missing input data
4238```
4339
44- ---
40+ ## Architecture Overview
4541
46- ## 🧠 Architecture Overview
4742| Component | Description |
48- | ------------| --------------|
49- | ` DotNetOutputToConsoleLogger ` | Core class that writes safe ` <script> ` tags to the HTTP response with sanitized console commands. |
50- | ` DotNetOutputToConsoleHttpModule ` | Global ASP.NET module that automatically logs ** unhandled exceptions** to the console. |
51- | ` web.config ` switch | Toggle feature ON/OFF without recompiling. |
52- | ` Json.Encode() ` | Ensures all messages are XSS-safe before rendering. |
43+ | ----------| -------------|
44+ | ` DotNetOutputToConsoleLogger ` | Core class that writes sanitized console commands into browser output. |
45+ | ` DotNetOutputToConsoleHttpModule ` | Automatically logs unhandled exceptions at the application level. |
46+ | ` web.config ` switch | Allows turning console output on or off globally. |
47+ | ` Json.Encode() ` | Sanitizes all messages to avoid XSS or script injection. |
48+
49+ ## Configuration
5350
54- ---
51+ Add the following to your Web.config:
5552
56- ## 📂 Configuration
57- Add this to your ** Web.config** :
5853``` xml
5954<configuration >
6055 <appSettings >
@@ -63,37 +58,39 @@ Add this to your **Web.config**:
6358
6459 <system .webServer>
6560 <modules >
66- <add name =" DotNetOutputToConsoleHttpModule" type =" DotNetOutputToConsole.DotNetOutputToConsoleHttpModule" />
61+ <add name =" DotNetOutputToConsoleHttpModule"
62+ type =" DotNetOutputToConsole.DotNetOutputToConsoleHttpModule" />
6763 </modules >
6864 </system .webServer>
6965</configuration >
7066```
7167
72- ✅ Set ` "EnableOutputToConsole" ` to ` "true" ` in UAT/DEV.
73- 🚫 Set to ` "false" ` in production for performance and security .
68+ Set ` "EnableOutputToConsole" ` to ` "true" ` in UAT or DEV.
69+ Set to ` "false" ` in production.
7470
75- ---
71+ ## Usage Examples
7672
77- ## 🧩 Usage Examples
78- ### 1️⃣ Log Information
73+ ### Log Information
7974``` csharp
8075DotNetOutputToConsoleLogger .LogInfo (" Page load completed" );
8176```
82- Console output:
77+
78+ Output:
8379```
8480INFO: Page load completed
8581```
8682
87- ### 2️⃣ Log Variables
83+ ### Log Variables
8884``` csharp
8985DotNetOutputToConsoleLogger .LogVariable (" Username" , user .Name );
9086```
91- Console output:
87+
88+ Output:
9289```
9390LOG: Username: JohnDoe
9491```
9592
96- ### 3️⃣ Log Exceptions
93+ ### Log Exceptions
9794``` csharp
9895try
9996{
@@ -104,28 +101,28 @@ catch (Exception ex)
104101 DotNetOutputToConsoleLogger .LogError (ex .Message );
105102}
106103```
107- Console output:
104+
105+ Output:
108106```
109107ERROR: Simulated failure
110108```
111109
112- ### 4️⃣ Automatic Error Logging (No Try/Catch Needed)
113- The built-in HTTP module automatically logs unhandled exceptions:
110+ ### Automatic Error Logging (Unhandled Exceptions)
114111``` csharp
115112protected void Page_Load (object sender , EventArgs e )
116113{
117114 throw new Exception (" Unhandled page error!" );
118115}
119116```
120- Console output:
117+
118+ Output:
121119```
122120ERROR: Unhandled Exception: Unhandled page error!
123121```
124122
125- ---
123+ ## Class Overview
126124
127- ## 🧩 Class Overview
128- ### 🔹 DotNetOutputToConsoleLogger.cs
125+ ### DotNetOutputToConsoleLogger.cs
129126``` csharp
130127public static class DotNetOutputToConsoleLogger
131128{
@@ -134,10 +131,8 @@ public static class DotNetOutputToConsoleLogger
134131 public static void LogError (string message );
135132}
136133```
137- All three use ` Json.Encode() ` internally to prevent XSS or script injection.
138134
139- ### 🔹 DotNetOutputToConsoleHttpModule.cs
140- Automatically hooks into the ASP.NET pipeline:
135+ ### DotNetOutputToConsoleHttpModule.cs
141136``` csharp
142137public class DotNetOutputToConsoleHttpModule : IHttpModule
143138{
@@ -153,54 +148,43 @@ public class DotNetOutputToConsoleHttpModule : IHttpModule
153148}
154149```
155150
156- ---
151+ ## Security
157152
158- ## 🔒 Security
159- | Protection | Description |
160- | -------------| --------------|
161- | ** XSS Safe** | Uses ` System.Web.Helpers.Json.Encode() ` to escape any special characters before writing scripts. |
162- | ** Response-Safe** | Only writes inside valid ` <script> ` tags. |
163- | ** Config Toggle** | Easily disable all console output via Web.config. |
164- | ** Recommended Use** | Only enable in DEV or UAT environments. |
153+ | Feature | Description |
154+ | --------| -------------|
155+ | XSS Protection | All messages pass through ` Json.Encode() ` to escape special characters. |
156+ | Safe Output | Only writes inside valid ` <script> ` blocks. |
157+ | Config Toggle | Can be disabled instantly using Web.config. |
158+ | Recommended Usage | Enable only in UAT/DEV. |
165159
166- ---
160+ ## Unit Testing
167161
168- ## 🧪 Unit Testing
169- ### Installation
162+ ### Test Framework Installation
170163``` powershell
171164Install-Package NUnit
172165Install-Package NUnit3TestAdapter
173166Install-Package Microsoft.NET.Test.Sdk
174167```
168+
175169### Example Test
176170``` csharp
177- using NUnit .Framework ;
178- using DotNetOutputToConsole ;
179-
180- namespace DotNetOutputToConsole .Tests
171+ [Test ]
172+ public void LogInfo_ShouldNotThrow ()
181173{
182- [TestFixture ]
183- public class DotNetOutputToConsoleLoggerTests
184- {
185- [Test ]
186- public void LogInfo_ShouldNotThrow () => Assert .DoesNotThrow (() => DotNetOutputToConsoleLogger .LogInfo (" info" ));
187- [Test ]
188- public void LogError_ShouldNotThrow () => Assert .DoesNotThrow (() => DotNetOutputToConsoleLogger .LogError (" error" ));
189- [Test ]
190- public void LogVariable_ShouldNotThrow () => Assert .DoesNotThrow (() => DotNetOutputToConsoleLogger .LogVariable (" Key" , " Value" ));
191- }
174+ Assert .DoesNotThrow (() => DotNetOutputToConsoleLogger .LogInfo (" info" ));
192175}
193176```
177+
194178### Run Tests
195- In Visual Studio 2022 → ** Test → Test Explorer → Run All Tests**
179+ Visual Studio 2022 → Test Explorer → Run All Tests
196180or CLI:
181+
197182``` bash
198- dotnet test tests/DotNetOutputToConsole.Tests/DotNetOutputToConsole.Tests.csproj
183+ dotnet test
199184```
200185
201- ---
186+ ## Project Structure
202187
203- ## 🧱 Project Structure
204188```
205189DotNetOutputToConsole/
206190├── src/
@@ -221,37 +205,16 @@ DotNetOutputToConsole/
221205 └── DotNetOutputToConsoleLoggerTests.cs
222206```
223207
224- ---
225-
226- ## 🧩 How It Works
227- 1 . ` DotNetOutputToConsoleLogger ` calls ` HttpContext.Current.Response.Write(...) `
228- 2 . It injects a ` <script> ` tag that runs a console command like:
229- ``` html
230- <script >console .log (" Your message" ); </script >
231- ```
232- 3 . All messages are escaped via ` Json.Encode() ` .
233- 4 . View messages in browser ** Developer Tools → Console tab** .
208+ ## How It Works
209+ 1 . The logger injects ` <script> ` tags into the HTTP response.
210+ 2 . Messages are encoded using ` Json.Encode() ` to prevent script injection.
211+ 3 . Output executes inside the browser console.
212+ 4 . View results using Developer Tools → Console.
234213
235- ---
214+ ## Author
215+ Created by: ** livedcode**
216+ GitHub: https://github.com/livedcode
217+ NuGet: https://www.nuget.org/profiles/livedcode
236218
237- ## 🧑💻 Author
238- ** Created by:** ` livedcode `
239- GitHub: [ https://github.com/livedcode ] ( https://github.com/livedcode )
240- NuGet: [ https://www.nuget.org/profiles/livedcode ] ( https://www.nuget.org/profiles/livedcode )
241-
242- ---
243-
244- ## 📜 License
219+ ## License
245220MIT License © 2025 livedcode
246-
247- ---
248-
249- ## 🧭 Future Enhancements
250- - Add ` LogWarning() ` → ` console.warn() `
251- - Dual logging (console + file)
252- - ASP.NET Core middleware support
253- - MVC exception filter integration
254-
255- ---
256-
257- > 💡 “The simplest and safest way to see your ASP.NET logs in the browser console.”
0 commit comments