Skip to content

Commit 6cae94a

Browse files
committed
Improve the README
1 parent 3d57330 commit 6cae94a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ You can use [Log4View](https://www.log4view.com) to look at log files produced w
66

77
## Getting started
88

9+
Add the [Serilog.Formatting.Log4Net](https://www.nuget.org/packages/Serilog.Formatting.Log4Net/) NuGet package to your project using the NuGet Package Manager or run the following command:
10+
11+
```
12+
dotnet add package Serilog.Formatting.Log4Net
13+
```
14+
915
**Serilog.Formatting.Log4Net** provides the `Log4NetTextFormatter` class which implements Serilog's [ITextFormatter](https://github.com/serilog/serilog/blob/v2.0.0/src/Serilog/Formatting/ITextFormatter.cs#L20-L31) interface.
1016

1117
Here's how to use it with a file sink in a simple *Hello World* app:
@@ -47,7 +53,7 @@ You can configure `Log4NetTextFormatter` in multiple ways, the fluent options bu
4753

4854
### Exception formatting
4955

50-
By default, Log4NetTextFormatter formats exception by calling [ToString()](https://docs.microsoft.com/en-us/dotnet/api/system.exception.tostring). You can customise this behaviour by setting your own formatting delegate. For exemple, you could use [Ben.Demystifier](https://github.com/benaadams/Ben.Demystifier/) like this:
56+
By default, Log4NetTextFormatter formats exception by calling [ToString()](https://docs.microsoft.com/en-us/dotnet/api/system.exception.tostring). You can customise this behaviour by setting your own formatting delegate. For example, you could use [Ben.Demystifier](https://github.com/benaadams/Ben.Demystifier/) like this:
5157

5258
```c#
5359
new Log4NetTextFormatter(c => c.UseExceptionFormatter(exception => exception.ToStringDemystified()))
@@ -125,13 +131,22 @@ Note that unlike other fluent configuration methods, this one can not be chained
125131

126132
### Combining options
127133

128-
You can also combine options, for example, both removing namespaces and using Ben.Demystifier for exception formatting:
134+
You can also combine options, for example, using [Ben.Demystifier](https://www.nuget.org/packages/Ben.Demystifier/) for exception formatting, filtering properties and using the log4j compatibility mode. This sample configuration sends logs as UDP packages over the network with [Serilog.Sinks.Udp](https://www.nuget.org/packages/Serilog.Sinks.Udp/) and are viewable with [Loginator](https://github.com/dabeku/Loginator):
129135

130136
```c#
137+
var appFileName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
138+
var processId = Process.GetCurrentProcess().Id;
131139
var formatter = new Log4NetTextFormatter(c => c
132-
.UseLog4NetXmlNamespace(null)
133140
.UseExceptionFormatter(exception => exception.ToStringDemystified())
141+
.UsePropertyFilter((_, name) => name.StartsWith("log4j"))
142+
.UseLog4JCompatibility()
134143
);
144+
Log.Logger = new LoggerConfiguration()
145+
.Enrich.WithProperty("log4japp", $"{appFileName}({processId})")
146+
.Enrich.WithProperty("log4jmachinename", Environment.MachineName)
147+
.Enrich.WithThreadId()
148+
.WriteTo.Udp("localhost", 7071, AddressFamily.InterNetwork, formatter)
149+
.CreateLogger();
135150
```
136151

137152
## Enrichers
@@ -162,7 +177,7 @@ Include the machine name in log4net events by using [Serilog.Enrichers.Environme
162177
var loggerConfiguration = new LoggerConfiguration().Enrich.WithMachineName();
163178
```
164179

165-
Combining these three enrichers wil produce a log event like this, including `thread`, `domain` and `username` attributes plus a `log4net:HostName` property containing the machine name:
180+
Combining these three enrichers will produce a log event like this, including `thread`, `domain` and `username` attributes plus a `log4net:HostName` property containing the machine name:
166181

167182
```xml
168183
<event timestamp="2020-06-28T10:07:33.314159+02:00" level="INFO" thread="1" domain="TheDomainName" username="TheUserName">

0 commit comments

Comments
 (0)