Skip to content

Commit 0f615eb

Browse files
committed
Update Readme & License
1 parent 6662563 commit 0f615eb

File tree

2 files changed

+278
-1
lines changed

2 files changed

+278
-1
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 livedcode
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 257 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,257 @@
1-
# DotNetOutputToConsole
1+
# DotNetOutputToConsole
2+
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.
4+
5+
---
6+
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:
21+
22+
```powershell
23+
Install-Package DotNetOutputToConsole
24+
```
25+
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.
31+
32+
Example:
33+
```csharp
34+
DotNetOutputToConsoleLogger.LogVariable("SessionId", Session.SessionID);
35+
DotNetOutputToConsoleLogger.LogError("Missing input data");
36+
```
37+
38+
Console output:
39+
```
40+
LOG: SessionId: 1a2b3c4d
41+
ERROR: Missing input data
42+
```
43+
44+
---
45+
46+
## 🧠 Architecture Overview
47+
| 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. |
53+
54+
---
55+
56+
## 📂 Configuration
57+
Add this to your **Web.config**:
58+
```xml
59+
<configuration>
60+
<appSettings>
61+
<add key="EnableOutputToConsole" value="true" />
62+
</appSettings>
63+
64+
<system.webServer>
65+
<modules>
66+
<add name="DotNetOutputToConsoleHttpModule" type="DotNetOutputToConsole.DotNetOutputToConsoleHttpModule" />
67+
</modules>
68+
</system.webServer>
69+
</configuration>
70+
```
71+
72+
✅ Set `"EnableOutputToConsole"` to `"true"` in UAT/DEV.
73+
🚫 Set to `"false"` in production for performance and security.
74+
75+
---
76+
77+
## 🧩 Usage Examples
78+
### 1️⃣ Log Information
79+
```csharp
80+
DotNetOutputToConsoleLogger.LogInfo("Page load completed");
81+
```
82+
Console output:
83+
```
84+
INFO: Page load completed
85+
```
86+
87+
### 2️⃣ Log Variables
88+
```csharp
89+
DotNetOutputToConsoleLogger.LogVariable("Username", user.Name);
90+
```
91+
Console output:
92+
```
93+
LOG: Username: JohnDoe
94+
```
95+
96+
### 3️⃣ Log Exceptions
97+
```csharp
98+
try
99+
{
100+
throw new Exception("Simulated failure");
101+
}
102+
catch (Exception ex)
103+
{
104+
DotNetOutputToConsoleLogger.LogError(ex.Message);
105+
}
106+
```
107+
Console output:
108+
```
109+
ERROR: Simulated failure
110+
```
111+
112+
### 4️⃣ Automatic Error Logging (No Try/Catch Needed)
113+
The built-in HTTP module automatically logs unhandled exceptions:
114+
```csharp
115+
protected void Page_Load(object sender, EventArgs e)
116+
{
117+
throw new Exception("Unhandled page error!");
118+
}
119+
```
120+
Console output:
121+
```
122+
ERROR: Unhandled Exception: Unhandled page error!
123+
```
124+
125+
---
126+
127+
## 🧩 Class Overview
128+
### 🔹 DotNetOutputToConsoleLogger.cs
129+
```csharp
130+
public static class DotNetOutputToConsoleLogger
131+
{
132+
public static void LogInfo(string message);
133+
public static void LogVariable(string name, object value);
134+
public static void LogError(string message);
135+
}
136+
```
137+
All three use `Json.Encode()` internally to prevent XSS or script injection.
138+
139+
### 🔹 DotNetOutputToConsoleHttpModule.cs
140+
Automatically hooks into the ASP.NET pipeline:
141+
```csharp
142+
public class DotNetOutputToConsoleHttpModule : IHttpModule
143+
{
144+
public void Init(HttpApplication context)
145+
{
146+
context.Error += (sender, e) =>
147+
{
148+
var ex = HttpContext.Current?.Server.GetLastError();
149+
if (ex != null)
150+
DotNetOutputToConsoleLogger.LogError($"Unhandled Exception: {ex.Message}");
151+
};
152+
}
153+
}
154+
```
155+
156+
---
157+
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. |
165+
166+
---
167+
168+
## 🧪 Unit Testing
169+
### Installation
170+
```powershell
171+
Install-Package NUnit
172+
Install-Package NUnit3TestAdapter
173+
Install-Package Microsoft.NET.Test.Sdk
174+
```
175+
### Example Test
176+
```csharp
177+
using NUnit.Framework;
178+
using DotNetOutputToConsole;
179+
180+
namespace DotNetOutputToConsole.Tests
181+
{
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+
}
192+
}
193+
```
194+
### Run Tests
195+
In Visual Studio 2022 → **Test → Test Explorer → Run All Tests**
196+
or CLI:
197+
```bash
198+
dotnet test tests/DotNetOutputToConsole.Tests/DotNetOutputToConsole.Tests.csproj
199+
```
200+
201+
---
202+
203+
## 🧱 Project Structure
204+
```
205+
DotNetOutputToConsole/
206+
├── src/
207+
│ └── DotNetOutputToConsole/
208+
│ ├── DotNetOutputToConsoleLogger.cs
209+
│ ├── DotNetOutputToConsoleHttpModule.cs
210+
│ ├── Properties/
211+
│ │ └── AssemblyInfo.cs
212+
│ ├── README.md
213+
│ └── LICENSE
214+
├── demo/
215+
│ └── DotNetOutputToConsole.DemoWeb/
216+
│ ├── Default.aspx
217+
│ ├── Default.aspx.cs
218+
│ └── Web.config
219+
└── tests/
220+
└── DotNetOutputToConsole.Tests/
221+
└── DotNetOutputToConsoleLoggerTests.cs
222+
```
223+
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**.
234+
235+
---
236+
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
245+
MIT 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

Comments
 (0)