Skip to content

Commit 0ced1ee

Browse files
authored
Merge pull request #38 from serilog/dev
2.0.0 Release
2 parents dd80bc6 + b8df615 commit 0ced1ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+428
-505
lines changed

README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ You can get started quickly with this package, and later migrate to the full Ser
1616
**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:
1717

1818
```xml
19-
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
19+
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
2020
```
2121

22-
**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`.
2323

2424
```csharp
25-
public void Configure(IApplicationBuilder app,
26-
IHostingEnvironment env,
27-
ILoggerFactory loggerFactory)
25+
WebHost.CreateDefaultBuilder(args)
26+
.ConfigureLogging((hostingContext, builder) =>
2827
{
29-
loggerFactory.AddFile("Logs/myapp-{Date}.txt");
28+
builder.AddFile("Logs/myapp-{Date}.txt");
29+
})
30+
.UseStartup<Startup>()
31+
.Build();
3032
```
3133

3234
**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
3537
class HomeController : Controller
3638
{
3739
readonly ILogger<HomeController> _log;
38-
40+
3941
public HomeController(ILogger<HomeController> log)
4042
{
4143
_log = log;
4244
}
43-
45+
4446
public IActionResult Index()
4547
{
4648
_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
6567
| **Level** | The log level assigned to the event. | Three-character code in brackets | `[INF]` |
6668
| **Message** | The log message associated with the event. | Free text | `Hello, world!` |
6769
| **Event id** | Identifies messages generated from the same format string/message template. | 32-bit hexadecimal, in parentheses | `(f83bcf75)` |
68-
| **Exception** | Exception associated with the event. | `Exception.ToString()` format (not shown) | `System.DivideByZeroException: Attempt to divide by zero\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...` |
6971

7072
To record events in newline-separated JSON instead, specify `isJson: true` when configuring the logger:
7173

7274
```csharp
73-
loggerFactory.AddFile("Logs/myapp-{Date}.txt", isJson: true);
75+
loggingBuilder.AddFile("Logs/myapp-{Date}.txt", isJson: true);
7476
```
7577

7678
This will produce a log file with lines like:
@@ -113,12 +115,13 @@ The `AddFile()` method exposes some basic options for controlling the connection
113115

114116
| Parameter | Description | Example value |
115117
| --------- | ----------- | ------------- |
116-
| `pathFormat` | Filname 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` |
118+
| `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` |
117119
| `minimumLevel` | The level below which events will be suppressed (the default is `LogLevel.Information`). | `LogLevel.Debug` |
118120
| `levelOverrides` | A dictionary mapping logger name prefixes to minimum logging levels. | |
119121
| `isJson` | If true, the log file will be written in JSON format. | `true` |
120122
| `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` |
121123
| `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}` |
122125

123126
### `appsettings.json` configuration
124127

@@ -141,7 +144,7 @@ In `appsettings.json` add a `"Logging"` property:
141144
And then pass the configuration section to the `AddFile()` method:
142145

143146
```csharp
144-
loggerFactory.AddFile(Configuration.GetSection("Logging"));
147+
loggingBuilder.AddFile(Configuration.GetSection("Logging"));
145148
```
146149

147150
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
151154
| `Json` | If `true`, the log file will be written in JSON format. | `true` |
152155
| `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` |
153156
| `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}` |
154158

155159
### Using the full Serilog API
156160

157-
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.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.
158162

159163
The following packages are used to provide `AddFile()`:
160164

@@ -163,5 +167,5 @@ The following packages are used to provide `AddFile()`:
163167
* [Serilog.Formatting.Compact](https://github.com/serilog/serilog-formatting-compact) - JSON event formatting
164168
* [Serilog.Extensions.Logging](https://github.com/serilog/serilog-extensions-logging) - ASP.NET Core integration
165169
* [Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) - async wrapper to perform log writes on a background thread
166-
170+
167171
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).

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ artifacts:
1010
deploy:
1111
- provider: NuGet
1212
api_key:
13-
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
13+
secure: T+8ZvBlF6teDbEuPRHVWMkuaU84MAygeuveqR4TqHbcBrW7bBOhtljzUNfiLYjfr
1414
skip_symbols: true
1515
on:
1616
branch: /^(master|dev)$/

example/WebApplication/Controllers/HomeController.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Mvc;
7+
using WebApplication.Models;
68
using Microsoft.Extensions.Logging;
79

810
namespace WebApplication.Controllers
911
{
1012
public class HomeController : Controller
1113
{
12-
readonly ILogger<HomeController> _log;
14+
private readonly ILogger<HomeController> _log;
1315

1416
public HomeController(ILogger<HomeController> log)
1517
{
@@ -39,7 +41,7 @@ public IActionResult Contact()
3941

4042
public IActionResult Error()
4143
{
42-
return View();
44+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
4345
}
4446
}
4547
}

example/WebApplication/Logs/log-20170630.txt

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2017-09-04T12:23:39.9205194+02:00 0HL7JFS1C6IAQ:00000001 [INF] Request starting HTTP/1.1 GET http://localhost:52126/ (ca22a1cb)
2+
2017-09-04T12:23:39.9685124+02:00 0HL7JFS1C6IAQ:00000001 [INF] Executing action method "WebApplication.Controllers.HomeController.Index (WebApplication)" with arguments (null) - ModelState is Valid (ba7f4ac2)
3+
2017-09-04T12:23:39.9689269+02:00 0HL7JFS1C6IAQ:00000001 [INF] Hello, world! (f83bcf75)
4+
2017-09-04T12:23:41.5391807+02:00 0HL7JFS1C6IAQ:00000001 [INF] Executing ViewResult, running view at path "/Views/Home/Index.cshtml". (9707eebe)
5+
2017-09-04T12:23:41.7710750+02:00 0HL7JFS1C6IAQ:00000001 [INF] Executed action "WebApplication.Controllers.HomeController.Index (WebApplication)" in 1807.3437ms (afa2e885)
6+
2017-09-04T12:23:41.7735962+02:00 0HL7JFS1C6IAQ:00000001 [INF] Request finished in 1854.3777ms 200 text/html; charset=utf-8 (791a596a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace WebApplication.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}

example/WebApplication/Program.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
67
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
710

811
namespace WebApplication
912
{
1013
public class Program
1114
{
1215
public static void Main(string[] args)
1316
{
14-
var host = new WebHostBuilder()
15-
.UseKestrel()
16-
.UseContentRoot(Directory.GetCurrentDirectory())
17-
.UseIISIntegration()
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.ConfigureLogging((hostingContext, builder) =>
23+
{
24+
var configuration = hostingContext.Configuration.GetSection("Logging");
25+
builder.AddFile(configuration);
26+
})
1827
.UseStartup<Startup>()
1928
.Build();
20-
21-
host.Run();
22-
}
2329
}
2430
}

example/WebApplication/Project_Readme.html

-187
This file was deleted.

0 commit comments

Comments
 (0)