concept log in category asp.net

appears as: logs, log, log, logs
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

Another common requirement is to have different amounts of logging depending on where your app is running. In development, it’s great to generate lots of logs as it helps debugging, but once you get to production, too much logging can be overwhelming. You’ll want to log warnings and errors, maybe information-level logs, but definitely not debug-level logs!

This infrastructure logging can be useful, but log messages that you create yourself can have even greater value. For example, you may be able to spot the cause of the error from the log messages in figure 17.1—we’re attempting to view a recipe with an unknown RecipeId of 0, but it’s far from obvious. If you explicitly add a log message to your app when this happens, as in figure 17.2, then the problem is much more apparent.

Figure 17.3. The components of the ASP.NET Core logging framework. You register logging providers with an ILoggerFactory, which are used to create implementations of ILogger. You write logs to the ILogger, which uses the ILogger implementations to output logs to the console or a file.

This design in figure 17.3 makes it easy to add or change where your application writes the log messages, without having to change your application code. This listing shows all the code required to add an ILoggerProvider that writes logs to the console.

Listing 17.6. Configuring a Serilog logging provider to use a console sink
public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() #1 .WriteTo.Console() #2 .CreateLogger(); #3 try { BuildWebHost(args).Run(); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); } finally { Log.CloseAndFlush(); } } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseSerilog() #4 .UseStartup<Startup>() .Build(); } #1 Creates a LoggerConfiguration for configuring the Serilog logger #2 Serilog will write logs to the console. #3 This creates a Serilog logger instance on the static Log.Logger property. #4 Registers the SerilogLoggerFactory and connects the Log.Logger as the sole logging provider !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":46}],[{\"line\":4,\"ch\":63},{\"line\":4,\"ch\":65}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":2}],[{\"line\":5,\"ch\":12},{\"line\":5,\"ch\":30}],[{\"line\":5,\"ch\":63},{\"line\":5,\"ch\":65}],[{\"line\":30,\"ch\":0},{\"line\":30,\"ch\":2}],[{\"line\":6,\"ch\":12},{\"line\":6,\"ch\":28}],[{\"line\":6,\"ch\":63},{\"line\":6,\"ch\":65}],[{\"line\":31,\"ch\":0},{\"line\":31,\"ch\":2}],[{\"line\":8,\"ch\":8},{\"line\":8,\"ch\":11}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":5}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":9}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":9}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":9}],[{\"line\":11,\"ch\":8},{\"line\":11,\"ch\":9}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":9}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":9}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":5}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":1}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":28}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":5}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":9}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":9}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":9}],[{\"line\":14,\"ch\":12},{\"line\":14,\"ch\":58}],[{\"line\":11,\"ch\":8},{\"line\":11,\"ch\":9}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":9}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":9}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":5}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":1}],[{\"line\":16,\"ch\":8},{\"line\":16,\"ch\":15}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":5}],[{\"line\":9,\"ch\":8},{\"line\":9,\"ch\":9}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":9}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":9}],[{\"line\":18,\"ch\":12},{\"line\":18,\"ch\":32}],[{\"line\":11,\"ch\":8},{\"line\":11,\"ch\":9}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":9}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":9}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":5}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":1}],[{\"line\":24,\"ch\":12},{\"line\":24,\"ch\":25}],[{\"line\":24,\"ch\":63},{\"line\":24,\"ch\":65}],[{\"line\":32,\"ch\":0},{\"line\":32,\"ch\":2}]]"} !@%STYLE%@!

With the Serilog logging provider configured, you can run the application and generate some logs. Every time the app generates a log, ILogger writes the message to the Serilog provider, which writes it to every sink. In the example, you’ve only configured the console sink, so the output will look something like figure 17.9. The Serilog console sink colorizes the logs more than the built-in console provider, so I find it’s somewhat easier to parse the logs visually.

  • You can configure multiple ILoggerProvider instances in ASP.NET Core, which define where logs are output. The CreateDefaultBuilder method adds the console and debug providers, and you can add additional providers by calling ConfigureLogging().