You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-14
Original file line number
Diff line number
Diff line change
@@ -16,17 +16,19 @@ You can get started quickly with this package, and later migrate to the full Ser
16
16
**1.** Add [the NuGet package](https://nuget.org/packages/serilog.extensions.logging.file) as a dependency of your project either with the package manager or directly to the CSPROJ file:
**2.** In your `Startup` class's `Configure()` method, call `AddFile()` on the provided `loggerFactory`.
22
+
**2.** In your `Program` class, configure logging on the web host builder, and call `AddFile()` on the provided `loggingBuilder`.
23
23
24
24
```csharp
25
-
publicvoidConfigure(IApplicationBuilderapp,
26
-
IHostingEnvironmentenv,
27
-
ILoggerFactoryloggerFactory)
25
+
WebHost.CreateDefaultBuilder(args)
26
+
.ConfigureLogging((hostingContext, builder) =>
28
27
{
29
-
loggerFactory.AddFile("Logs/myapp-{Date}.txt");
28
+
builder.AddFile("Logs/myapp-{Date}.txt");
29
+
})
30
+
.UseStartup<Startup>()
31
+
.Build();
30
32
```
31
33
32
34
**Done!** The framework will inject `ILogger` instances into controllers and other classes:
@@ -35,12 +37,12 @@ You can get started quickly with this package, and later migrate to the full Ser
35
37
classHomeController : Controller
36
38
{
37
39
readonlyILogger<HomeController> _log;
38
-
40
+
39
41
publicHomeController(ILogger<HomeController> log)
40
42
{
41
43
_log=log;
42
44
}
43
-
45
+
44
46
publicIActionResultIndex()
45
47
{
46
48
_log.LogInformation("Hello, world!");
@@ -65,12 +67,12 @@ By default, the file will be written in plain text. The fields in the log file a
65
67
|**Level**| The log level assigned to the event. | Three-character code in brackets |`[INF]`|
66
68
|**Message**| The log message associated with the event. | Free text |`Hello, world!`|
67
69
|**Event id**| Identifies messages generated from the same format string/message template. | 32-bit hexadecimal, in parentheses |`(f83bcf75)`|
68
-
|**Exception**|Exceptionassociatedwiththeevent. | `Exception.ToString()` format (notshown) | `System.DivideByZeroException:Attempttodividebyzero\r\n\ at...` |
70
+
|**Exception**| Exception associated with the event. |`Exception.ToString()` format (not shown) |`System.DivideByZeroException: Attempt to divide by zero\r\n\ at...`|
69
71
70
72
To record events in newline-separated JSON instead, specify `isJson: true` when configuring the logger:
|`pathFormat`|Filename to write. The filename may include `{Date}` to specify how the date portion of the filename is calculated. May include environment variables.|`Logs/log-{Date}.txt`|
117
119
|`minimumLevel`| The level below which events will be suppressed (the default is `LogLevel.Information`). |`LogLevel.Debug`|
118
120
|`levelOverrides`| A dictionary mapping logger name prefixes to minimum logging levels. ||
119
121
|`isJson`| If true, the log file will be written in JSON format. |`true`|
120
122
|`fileSizeLimitBytes`| The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, pass`null`. The default is 1 GiB. |`1024 * 1024 * 1024`|
121
123
|`retainedFileCountLimit`| The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass `null`. The default is `31`. |`31`|
124
+
|`outputTemplate`| The template used for formatting plain text log output. The default is `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}`|`{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}`|
122
125
123
126
### `appsettings.json` configuration
124
127
@@ -141,7 +144,7 @@ In `appsettings.json` add a `"Logging"` property:
141
144
And then pass the configuration section to the `AddFile()` method:
In addition to the properties shown above, the `"Logging"` configuration supports:
@@ -151,10 +154,11 @@ In addition to the properties shown above, the `"Logging"` configuration support
151
154
|`Json`| If `true`, the log file will be written in JSON format. |`true`|
152
155
|`FileSizeLimitBytes`| The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, pass`null`. The default is 1 GiB. |`1024 * 1024 * 1024`|
153
156
|`RetainedFileCountLimit`| The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass `null`. The default is `31`. |`31`|
157
+
|`OutputTemplate`| The template used for formatting plain text log output. The default is `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}`|`{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}`|
154
158
155
159
### Using the full Serilog API
156
160
157
-
Thispackageisopinionated, providingthemostcommon/recommendedoptionssupportedbySerilog. Formoresophisticatedconfiguration, usingSerilogdirectlyisrecommened. Seetheinstructionsin [Serilog.Extensions.Logging](https://github.com/serilog/serilog-extensions-logging) to get started.
161
+
This package is opinionated, providing the most common/recommended options supported by Serilog. For more sophisticated configuration, using Serilog directly is recommened. See the instructions in [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore) to get started.
158
162
159
163
The following packages are used to provide `AddFile()`:
160
164
@@ -163,5 +167,5 @@ The following packages are used to provide `AddFile()`:
*[Serilog.Extensions.Logging](https://github.com/serilog/serilog-extensions-logging) - ASP.NET Core integration
165
169
*[Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) - async wrapper to perform log writes on a background thread
166
-
170
+
167
171
If you decide to switch to the full Serilog API and need help, please drop into the [Gitter channel](https://gitter.im/serilog/serilog) or post your question on [Stack Overflow](http://stackoverflow.com/questions/tagged/serilog).
0 commit comments