Skip to content

2.0.0 Release #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ You can get started quickly with this package, and later migrate to the full Ser
**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:

```xml
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
```

**2.** In your `Startup` class's `Configure()` method, call `AddFile()` on the provided `loggerFactory`.
**2.** In your `Program` class, configure logging on the web host builder, and call `AddFile()` on the provided `loggingBuilder`.

```csharp
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, builder) =>
{
loggerFactory.AddFile("Logs/myapp-{Date}.txt");
builder.AddFile("Logs/myapp-{Date}.txt");
})
.UseStartup<Startup>()
.Build();
```

**Done!** The framework will inject `ILogger` instances into controllers and other classes:
Expand All @@ -35,12 +37,12 @@ You can get started quickly with this package, and later migrate to the full Ser
class HomeController : Controller
{
readonly ILogger<HomeController> _log;

public HomeController(ILogger<HomeController> log)
{
_log = log;
}

public IActionResult Index()
{
_log.LogInformation("Hello, world!");
Expand All @@ -65,12 +67,12 @@ By default, the file will be written in plain text. The fields in the log file a
| **Level** | The log level assigned to the event. | Three-character code in brackets | `[INF]` |
| **Message** | The log message associated with the event. | Free text | `Hello, world!` |
| **Event id** | Identifies messages generated from the same format string/message template. | 32-bit hexadecimal, in parentheses | `(f83bcf75)` |
| **Exception** | Exception associated with the event. | `Exception.ToString()` format (not shown) | `System.DivideByZeroException: Attempt to divide by zero\r\n\ at...` |
| **Exception** | Exception associated with the event. | `Exception.ToString()` format (not shown) | `System.DivideByZeroException: Attempt to divide by zero\r\n\ at...` |

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

```csharp
loggerFactory.AddFile("Logs/myapp-{Date}.txt", isJson: true);
loggingBuilder.AddFile("Logs/myapp-{Date}.txt", isJson: true);
```

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

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

### `appsettings.json` configuration

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

```csharp
loggerFactory.AddFile(Configuration.GetSection("Logging"));
loggingBuilder.AddFile(Configuration.GetSection("Logging"));
```

In addition to the properties shown above, the `"Logging"` configuration supports:
Expand All @@ -151,10 +154,11 @@ In addition to the properties shown above, the `"Logging"` configuration support
| `Json` | If `true`, the log file will be written in JSON format. | `true` |
| `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` |
| `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` |
| `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}` |

### Using the full Serilog API

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

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

Expand All @@ -163,5 +167,5 @@ The following packages are used to provide `AddFile()`:
* [Serilog.Formatting.Compact](https://github.com/serilog/serilog-formatting-compact) - JSON event formatting
* [Serilog.Extensions.Logging](https://github.com/serilog/serilog-extensions-logging) - ASP.NET Core integration
* [Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) - async wrapper to perform log writes on a background thread

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).
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ artifacts:
deploy:
- provider: NuGet
api_key:
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
secure: T+8ZvBlF6teDbEuPRHVWMkuaU84MAygeuveqR4TqHbcBrW7bBOhtljzUNfiLYjfr
skip_symbols: true
on:
branch: /^(master|dev)$/
Expand Down
6 changes: 4 additions & 2 deletions example/WebApplication/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication.Models;
using Microsoft.Extensions.Logging;

namespace WebApplication.Controllers
{
public class HomeController : Controller
{
readonly ILogger<HomeController> _log;
private readonly ILogger<HomeController> _log;

public HomeController(ILogger<HomeController> log)
{
Expand Down Expand Up @@ -39,7 +41,7 @@ public IActionResult Contact()

public IActionResult Error()
{
return View();
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
6 changes: 0 additions & 6 deletions example/WebApplication/Logs/log-20170630.txt

This file was deleted.

6 changes: 6 additions & 0 deletions example/WebApplication/Logs/log-20170904.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2017-09-04T12:23:39.9205194+02:00 0HL7JFS1C6IAQ:00000001 [INF] Request starting HTTP/1.1 GET http://localhost:52126/ (ca22a1cb)
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)
2017-09-04T12:23:39.9689269+02:00 0HL7JFS1C6IAQ:00000001 [INF] Hello, world! (f83bcf75)
2017-09-04T12:23:41.5391807+02:00 0HL7JFS1C6IAQ:00000001 [INF] Executing ViewResult, running view at path "/Views/Home/Index.cshtml". (9707eebe)
2017-09-04T12:23:41.7710750+02:00 0HL7JFS1C6IAQ:00000001 [INF] Executed action "WebApplication.Controllers.HomeController.Index (WebApplication)" in 1807.3437ms (afa2e885)
2017-09-04T12:23:41.7735962+02:00 0HL7JFS1C6IAQ:00000001 [INF] Request finished in 1854.3777ms 200 text/html; charset=utf-8 (791a596a)
11 changes: 11 additions & 0 deletions example/WebApplication/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace WebApplication.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
20 changes: 13 additions & 7 deletions example/WebApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApplication
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, builder) =>
{
var configuration = hostingContext.Configuration.GetSection("Logging");
builder.AddFile(configuration);
})
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
187 changes: 0 additions & 187 deletions example/WebApplication/Project_Readme.html

This file was deleted.

Loading