Skip to content

Commit 9de80b9

Browse files
committed
Improve README
1 parent 9959247 commit 9de80b9

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![NuGet](https://img.shields.io/nuget/v/Serilog.Formatting.Log4Net.svg?label=NuGet&logo=NuGet)](https://www.nuget.org/packages/Serilog.Formatting.Log4Net/) [![Continuous Integration](https://img.shields.io/github/workflow/status/0xced/serilog-formatting-log4net/Continuous%20Integration?label=Continuous%20Integration&logo=GitHub)](https://github.com/0xced/serilog-formatting-log4net/actions?query=workflow%3A%22Continuous+Integration%22) [![Coverage](https://img.shields.io/codecov/c/github/0xced/serilog-formatting-log4net?label=Coverage&logo=Codecov&logoColor=f5f5f5)](https://codecov.io/gh/0xced/serilog-formatting-log4net)
44

5+
You can use [Log4View](https://www.log4view.com) to look at log files produced with this formatter.
6+
57
## Getting started
68

79
**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.
@@ -67,6 +69,28 @@ You can remove the `log4net` XML namespace by setting the `Log4NetXmlNamespace`
6769
new Log4NetTextFormatter(c => c.UseLog4NetXmlNamespace(null))
6870
```
6971

72+
#### Line ending
73+
74+
By default, Log4NetTextFormatter uses the line feed (LF) character for line ending between XML elements. You can choose to use CRLF if you need to:
75+
76+
```c#
77+
new Log4NetTextFormatter(c => c.UseLineEnding(LineEnding.CarriageReturn | LineEnding.LineFeed))
78+
```
79+
80+
#### Indentation
81+
82+
By default, Log4NetTextFormatter indents XML elements with two spaces. You can configure it to use either spaces or tabs. For example, indent XML elements with one tab:
83+
84+
```c#
85+
new Log4NetTextFormatter(c => c.UseIndentationSettings(new IndentationSettings(Indentation.Tab, 1)))
86+
```
87+
88+
Or you can use no indentation at all, having log4net events written on a single line:
89+
90+
```c#
91+
new Log4NetTextFormatter(c => c.UseNoIndentation())
92+
```
93+
7094
#### Format provider
7195

7296
By default, Log4NetTextFormatter uses the invariant culture (Serilog's default) when formatting Serilog properties that implement the `IFormattable` interface. It can be configured to use culture-specific formatting information. For example, to use the Swiss French culture:
@@ -88,15 +112,15 @@ new Log4NetTextFormatter(c => c.UsePropertyFilter((_, name) => name != "MySecret
88112
You can also combine options, for example, both removing namespaces and using Ben.Demystifier for exception formatting:
89113

90114
```c#
91-
new Log4NetTextFormatter(c => c
115+
var formatter = new Log4NetTextFormatter(c => c
92116
.UseLog4NetXmlNamespace(null)
93117
.UseExceptionFormatter(exception => exception.ToStringDemystified())
94118
);
95119
```
96120

97121
## Enrichers
98122

99-
The log4Net XML format defines some special attributes which are not included by default in Serilog events. They can be added by using the appropriate Serilog enrichers.
123+
The log4Net XML format defines some special attributes which are not included by default in Serilog events. They can be added by using the appropriate Serilog [enrichers](https://github.com/serilog/serilog/wiki/Enrichment).
100124

101125
#### Thread Id
102126

@@ -121,3 +145,20 @@ Include the machine name in log4net events by using [Serilog.Enrichers.Environme
121145
```c#
122146
var loggerConfiguration = new LoggerConfiguration().Enrich.WithMachineName();
123147
```
148+
149+
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:
150+
151+
```xml
152+
<event timestamp="2020-06-28T10:07:33.314159+02:00" level="INFO" thread="1" domain="TheDomainName" username="TheUserName">
153+
<properties>
154+
<data name="log4net:HostName" value="TheMachineName" />
155+
</properties>
156+
<message>The message</message>
157+
</event>
158+
```
159+
160+
## Related projects
161+
162+
The [Serilog.Sinks.Log4Net](https://github.com/serilog/serilog-sinks-log4net) project is similar but depends on the log4net NuGet package whereas Serilog.Formatting.Log4Net does not. Also, Serilog.Sinks.Log4Net is a sink so you have to configure log4net in addition to configuring Serilog.
163+
164+
The [Serilog.Sinks.Udp](https://github.com/FantasticFiasco/serilog-sinks-udp) project also provides a [Log4Net formatter](https://github.com/FantasticFiasco/serilog-sinks-udp/blob/v7.1.0/src/Serilog.Sinks.Udp/Sinks/Udp/TextFormatters/Log4netTextFormatter.cs) but it writes XML *manually* (without using an [XmlWriter](https://docs.microsoft.com/en-us/dotnet/api/System.Xml.XmlWriter)), completely ignores Serilog properties, is not configurable at all (indentation, newlines, namespaces etc.) and is not documented.

0 commit comments

Comments
 (0)