Serilog levels can be overridden per logging source as below, for example we only want to filter Microsoft and System category from Level Information and below.
Much like TestsController.cs we inject an instance of ILogger and perform logging inside of GET request.
If we run our app now and consume the API endpoint http://localhost/api/tests, we should end up with some logging results in the console.
Create a Logger Extensions
This is an optional extension for logger to define custom logging messages.
usingMicrosoft.Extensions.Logging;usingSystem;publicstaticclassLoggerExtensions{ /// <summary> /// Formats and writes a trace log message with a default event id. /// </summary> /// <paramname="logger">The <seecref="ILogger"/> to write to.</param> /// <paramname="exception">The exception to log.</param> /// <paramname="message">Format string of the log message.</param> /// <paramname="args">An object array that contains zero or more objects to format.</param>publicstaticvoidLogTrace(thisILogger logger,Exception exception,string message =null,paramsobject[] args) {logger.LogTrace(default(EventId), exception, message, args); } /// <summary> /// Formats and writes a information log message with a default event id. /// </summary> /// <paramname="logger">The <seecref="ILogger"/> to write to.</param> /// <paramname="exception">The exception to log.</param> /// <paramname="message">Format string of the log message.</param> /// <paramname="args">An object array that contains zero or more objects to format.</param> public static void LogInformation(this ILogger logger, Exception exception, string message = null, params object[] args)
{ logger.LogInformation(default(EventId), exception, message, args); } /// <summary> /// Formats and writes a warning log message with a default event id. /// </summary> /// <paramname="logger">The <seecref="ILogger"/> to write to.</param> /// <paramname="exception">The exception to log.</param> /// <paramname="message">Format string of the log message.</param> /// <paramname="args">An object array that contains zero or more objects to format.</param> public static void LogWarning(this ILogger logger, Exception exception, string message = null, params object[] args)
{logger.LogWarning(default(EventId), exception, message, args); } /// <summary> /// Formats and writes a error log message with a default event id. /// </summary> /// <paramname="logger">The <seecref="ILogger"/> to write to.</param> /// <paramname="exception">The exception to log.</param> /// <paramname="message">Format string of the log message.</param> /// <paramname="args">An object array that contains zero or more objects to format.</param>publicstaticvoidLogError(thisILogger logger,Exception exception,string message =null,paramsobject[] args) {logger.LogError(default(EventId), exception, message, args); } /// <summary> /// Formats and writes a critical log message with a default event id. /// </summary> /// <paramname="logger">The <seecref="ILogger"/> to write to.</param> /// <paramname="exception">The exception to log.</param> /// <paramname="message">Format string of the log message.</param> /// <paramname="args">An object array that contains zero or more objects to format.</param> public static void LogCritical(this ILogger logger, Exception exception, string message = null, params object[] args)
{logger.LogCritical(default(EventId), exception, message, args); }}