|
1 |
| -# serilog-aspnetcore |
2 |
| -Serilog integration for ASP.NET Core 2+ |
| 1 | +# Serilog.AspNetCore [](https://ci.appveyor.com/project/serilog/serilog-framework-logging/history) [](https://www.nuget.org/packages/Serilog.Extensions.Logging/) |
| 2 | + |
| 3 | + |
| 4 | +Serilog logging for ASP.NET Core. This package routes ASP.NET Core log messages through Serilog, so you can get information about ASP.NET's internal operations logged to the same Serilog sinks as your application events. |
| 5 | + |
| 6 | +### Instructions |
| 7 | + |
| 8 | +**First**, install the _Serilog.AspNetCore_ [NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app. You will need a way to view the log messages - _Serilog.Sinks.Console_ writes these to the console, and _Serilog.Sinks.Debug_ writes to the Visual Studio output window; there are [many more sinks available](https://www.nuget.org/packages?q=Tags%3A%22serilog%22) on NuGet. |
| 9 | + |
| 10 | +```powershell |
| 11 | +Install-Package Serilog.AspNetCore -DependencyVersion Highest |
| 12 | +Install-Package Serilog.Sinks.Console |
| 13 | +Install-Package Serilog.Sinks.Debug |
| 14 | +``` |
| 15 | + |
| 16 | +**Next**, in your application's _Program.cs_ file, configure Serilog first: |
| 17 | + |
| 18 | +```csharp |
| 19 | +public class Program |
| 20 | +{ |
| 21 | + public static int Main(string[] args) |
| 22 | + { |
| 23 | + Log.Logger = new LoggerConfiguration() |
| 24 | + .MinimumLevel.Debug() |
| 25 | + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) |
| 26 | + .Enrich.FromLogContext() |
| 27 | + .WriteTo.Console() |
| 28 | + .WriteTo.Debug() |
| 29 | + .CreateLogger(); |
| 30 | +``` |
| 31 | + |
| 32 | +Then, add `UseSerilog()` to the web host builder. A `try`/`catch` block will ensure any configuration issues are appropriately logged: |
| 33 | + |
| 34 | +```csharp |
| 35 | + try |
| 36 | + { |
| 37 | + Log.Information("Starting web host"); |
| 38 | + |
| 39 | + var host = new WebHostBuilder() |
| 40 | + .UseKestrel() |
| 41 | + .UseContentRoot(Directory.GetCurrentDirectory()) |
| 42 | + .UseIISIntegration() |
| 43 | + .UseStartup<Startup>() |
| 44 | + .UseSerilog() // <-- Add this line |
| 45 | + .Build(); |
| 46 | + |
| 47 | + host.Run(); |
| 48 | + |
| 49 | + return 0; |
| 50 | + } |
| 51 | + catch (Exception ex) |
| 52 | + { |
| 53 | + Log.Fatal(ex, "Host terminated unexpectedly"); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + finally |
| 57 | + { |
| 58 | + Log.CloseAndFlush(); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +**Finally**, clean up by removing the remaining configuration for the default logger: |
| 65 | + |
| 66 | + * Remove calls to `AddLogging()` |
| 67 | + * Remove the `"Logging"` section from _appsettings.json_ files (this can be replaced with [Serilog configuration](https://github.com/serilog/serilog-settings-configuration) as shown in [this example](), if required) |
| 68 | + * Remove `ILoggerFactory` parameters and any `Add*()` calls on the logger factory in _Startup.cs_ |
| 69 | + * Remove `UseApplicationInsights()` (this can be replaced with the [Serilog AI sink](https://github.com/serilog/serilog-sinks-applicationinsights), if required) |
| 70 | + |
| 71 | +That's it! With the level bumped up a little you will see log output like: |
| 72 | + |
| 73 | +``` |
| 74 | +[22:14:44.646 DBG] RouteCollection.RouteAsync |
| 75 | + Routes: |
| 76 | + Microsoft.AspNet.Mvc.Routing.AttributeRoute |
| 77 | + {controller=Home}/{action=Index}/{id?} |
| 78 | + Handled? True |
| 79 | +[22:14:44.647 DBG] RouterMiddleware.Invoke |
| 80 | + Handled? True |
| 81 | +[22:14:45.706 DBG] /lib/jquery/jquery.js not modified |
| 82 | +[22:14:45.706 DBG] /css/site.css not modified |
| 83 | +[22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css |
| 84 | +``` |
| 85 | + |
| 86 | +### Using the package |
| 87 | + |
| 88 | +With _Serilog.AspNetCore_ installed and configured, you can write log messages directly through Serilog or any `ILogger` interface injected by ASP.NET. All loggers will use the same underlying implementation, levels, and destinations. |
0 commit comments