diff --git a/aspnetcore/fundamentals/logging/index.md b/aspnetcore/fundamentals/logging/index.md index ea288f05b08b..aa30f0771add 100644 --- a/aspnetcore/fundamentals/logging/index.md +++ b/aspnetcore/fundamentals/logging/index.md @@ -5,7 +5,7 @@ description: Learn how to use the logging framework provided by the Microsoft.Ex monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 10/08/2019 +ms.date: 11/05/2019 uid: fundamentals/logging/index --- # Logging in .NET Core and ASP.NET Core @@ -125,6 +125,69 @@ To write logs in the `Program` class of an ASP.NET Core app, get an `ILogger` in [!code-csharp[](index/samples/3.x/TodoApiSample/Program.cs?name=snippet_LogFromMain&highlight=9,10)] +Logging during host construction isn't directly supported. However, a separate logger can be used. In the following example, a [Serilog](https://serilog.net/) logger is used to log in `CreateHostBuilder`. `AddSerilog` uses the static configuration specified in `Log.Logger`: + +```csharp +using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +public class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) + { + var builtConfig = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddCommandLine(args) + .Build(); + + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .WriteTo.File(builtConfig["Logging:FilePath"]) + .CreateLogger(); + + try + { + return Host.CreateDefaultBuilder(args) + .ConfigureServices((context, services) => + { + services.AddRazorPages(); + }) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.AddConfiguration(builtConfig); + }) + .ConfigureLogging(logging => + { + logging.AddSerilog(); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } + catch (Exception ex) + { + Log.Fatal(ex, "Host builder error"); + + throw; + } + finally + { + Log.CloseAndFlush(); + } + } +} +``` + ### Create logs in the Startup class To write logs in the `Startup.Configure` method of an ASP.NET Core app, include an `ILogger` parameter in the method signature: @@ -162,6 +225,66 @@ To write logs in the `Program` class, get an `ILogger` instance from DI: [!code-csharp[](index/samples/2.x/TodoApiSample/Program.cs?name=snippet_LogFromMain&highlight=9,10)] +Logging during host construction isn't directly supported. However, a separate logger can be used. In the following example, a [Serilog](https://serilog.net/) logger is used to log in `CreateWebHostBuilder`. `AddSerilog` uses the static configuration specified in `Log.Logger`: + +```csharp +using System; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +public class Program +{ + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + { + var builtConfig = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddCommandLine(args) + .Build(); + + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .WriteTo.File(builtConfig["Logging:FilePath"]) + .CreateLogger(); + + try + { + return WebHost.CreateDefaultBuilder(args) + .ConfigureServices((context, services) => + { + services.AddMvc(); + }) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.AddConfiguration(builtConfig); + }) + .ConfigureLogging(logging => + { + logging.AddSerilog(); + }) + .UseStartup(); + } + catch (Exception ex) + { + Log.Fatal(ex, "Host builder error"); + + throw; + } + finally + { + Log.CloseAndFlush(); + } + } +} +``` + ::: moniker-end ### No asynchronous logger methods