|
| 1 | +# Log4net |
| 2 | + |
| 3 | +The [`SimpleW.Helper.Log4net`](https://www.nuget.org/packages/SimpleW.Helper.Log4net) package provides a **bridge between the SimpleW internal logging system and log4net**. |
| 4 | + |
| 5 | +It allows all logs emitted through `SimpleW.Observability.Log` to be automatically forwarded to a **log4net** `ILog` instance. |
| 6 | + |
| 7 | +This integration keeps the **SimpleW logging API lightweight and allocation-friendly**, while letting developers use the mature log4net ecosystem (appenders, layouts, filters, etc.). |
| 8 | + |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +It allows you to : |
| 13 | +- Forward all **SimpleW logs** to log4net |
| 14 | +- Automatically **detect the minimum logging level** from the provided log4net logger |
| 15 | +- Preserve **TraceId / SpanId** from `System.Diagnostics.Activity` |
| 16 | +- Attach the SimpleW **log source** as a log4net property |
| 17 | +- Add **custom enrichers** |
| 18 | + |
| 19 | + |
| 20 | +## Requirements |
| 21 | + |
| 22 | +- .NET 8.0 |
| 23 | +- SimpleW (core server) |
| 24 | +- Log4net (>= 3.2) |
| 25 | + |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +```sh |
| 30 | +$ dotnet add package SimpleW.Helper.Log4net --version 26.0.0-beta.20260202-1339 |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +## Configuration options |
| 35 | + |
| 36 | +| Option | Default | Description | |
| 37 | +| ------------------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 38 | +| MinimumLevel | `auto` | Overrides the SimpleW global minimum log level. When `null`, the level is automatically detected from the provided log4net logger. | |
| 39 | +| Mode | `SinkMode.Add` | Determines how the log4net bridge is attached. `Add` chains the sink with existing SimpleW sinks. `Replace` replaces the current SimpleW sink entirely. | |
| 40 | +| IncludeSource | `true` | When enabled, the SimpleW log source (`entry.Source`) is attached to the log4net context properties. | |
| 41 | +| IncludeTraceAndSpan | `true` | When an active `System.Diagnostics.Activity` exists, the bridge adds `trace_id` and `span_id` properties to the log event. | |
| 42 | +| SourcePropertyName | `"simplew_source"` | Name of the log4net property used to store the SimpleW log source. | |
| 43 | +| TraceIdPropertyName | `"trace_id"` | Name of the property used to store the `Activity.TraceId`. | |
| 44 | +| SpanIdPropertyName | `"span_id"` | Name of the property used to store the `Activity.SpanId`. | |
| 45 | +| Enricher | `null` | Optional callback allowing custom enrichment of the log4net properties for each log entry. Receives `(IDictionary<string, object?> properties, LogEntry entry)`. | |
| 46 | + |
| 47 | + |
| 48 | +## Minimal Example |
| 49 | + |
| 50 | +Configure a simple log4net console logger in code : |
| 51 | + |
| 52 | +```csharp |
| 53 | +using log4net; |
| 54 | +using log4net.Appender; |
| 55 | +using log4net.Layout; |
| 56 | +using log4net.Repository.Hierarchy; |
| 57 | +using SimpleW.Helper.Log4net; |
| 58 | + |
| 59 | +var hierarchy = (Hierarchy)LogManager.GetRepository(); |
| 60 | + |
| 61 | +var layout = new PatternLayout("%date %-5level %message%newline"); |
| 62 | +layout.ActivateOptions(); |
| 63 | + |
| 64 | +var consoleAppender = new ConsoleAppender { |
| 65 | + Layout = layout |
| 66 | +}; |
| 67 | +consoleAppender.ActivateOptions(); |
| 68 | + |
| 69 | +hierarchy.Root.AddAppender(consoleAppender); |
| 70 | +hierarchy.Root.Level = log4net.Core.Level.Info; |
| 71 | +hierarchy.Configured = true; |
| 72 | + |
| 73 | +ILog logger = LogManager.GetLogger(typeof(Program)); |
| 74 | +``` |
| 75 | + |
| 76 | +Install the bridge : |
| 77 | + |
| 78 | +```csharp |
| 79 | +var bridge = SimpleWLog4NetBridge.Install(logger, configure: options => { |
| 80 | + options.IncludeSource = true; |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +Once installed, every log emitted via : |
| 85 | + |
| 86 | +```csharp |
| 87 | +Log.Info("MySource", "Hello world"); |
| 88 | +``` |
| 89 | + |
| 90 | +will be forwarded to Log4net. |
| 91 | + |
| 92 | + |
| 93 | +## Automatic Minimum Level Detection |
| 94 | + |
| 95 | +When installing the bridge, SimpleW automatically detects the minimum level enabled in the log4net logger. |
| 96 | + |
| 97 | +Example : |
| 98 | + |
| 99 | +```csharp |
| 100 | +hierarchy.Root.Level = log4net.Core.Level.Warn; |
| 101 | +``` |
| 102 | + |
| 103 | +The bridge will automatically configure: |
| 104 | + |
| 105 | +```csharp |
| 106 | +SimpleW.Log.MinimumLevel = Warning |
| 107 | +``` |
| 108 | + |
| 109 | +This ensures that disabled log levels are filtered **before log creation**, preserving the performance characteristics of SimpleW logging. |
| 110 | + |
| 111 | + |
| 112 | +## Replace vs Add Sink |
| 113 | + |
| 114 | +By default the bridge adds a sink. |
| 115 | + |
| 116 | +```csharp |
| 117 | +options.Mode = SinkMode.Add; |
| 118 | +``` |
| 119 | + |
| 120 | +This allows multiple logging targets. |
| 121 | + |
| 122 | +Example : |
| 123 | + |
| 124 | +``` |
| 125 | +SimpleW |
| 126 | + ├─ Console sink |
| 127 | + └─ log4net bridge |
| 128 | +``` |
| 129 | + |
| 130 | +If you want Log4net to be the only logging target, replace the sink: |
| 131 | + |
| 132 | +```csharp |
| 133 | +SimpleWLog4netBridge.Install(logger, replaceExistingSink: true); |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +## Activity / Distributed Tracing |
| 138 | + |
| 139 | +When a `System.Diagnostics.Activity` is active (for example when using OpenTelemetry), the bridge automatically attaches : |
| 140 | + |
| 141 | +``` |
| 142 | +trace_id |
| 143 | +span_id |
| 144 | +``` |
| 145 | + |
| 146 | +These properties are stored in **log4net LogicalThreadContext properties**, allowing layouts such as : |
| 147 | + |
| 148 | +``` |
| 149 | +%date %-5level %property{trace_id} %property{span_id} %message%newline |
| 150 | +``` |
| 151 | + |
| 152 | +This allows log4net logs to correlate with distributed traces. |
| 153 | + |
| 154 | +SimpleW telemetry is based on `System.Diagnostics.Activity` and integrates naturally with the OpenTelemetry ecosystem. |
0 commit comments