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.
The example above will use a static logger that does not record events anywhere.
Using a Logger
Create an API controller TestsController.cs. We now are injecting an instance of ILogger. Of which we are using to Logging inside our Get() method.
Update the TestsController class with the following.
[Route("api/[controller]")]
public class TestsController : Controller
{
private readonly ILogger<TestsController> _logger;
public TestsController(ILogger<TestsController> logger)
{
_logger = logger;
}
// GET api/tests
[HttpGet]
public string Get()
{
_logger.LogTrace("LogTrace");
_logger.LogInformation("LogInformation");
_logger.LogWarning("LogWarning");
_logger.LogError("LogWarning");
_logger.LogCritical("LogCritical");
return Ok("Success");
}
}
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.
using Microsoft.Extensions.Logging;
using System;
public static class LoggerExtensions
{
/// <summary>
/// Formats and writes a trace log message with a default event id.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public static void LogTrace(this ILogger logger, Exception exception, string message = null, params object[] args)
{
logger.LogTrace(default(EventId), exception, message, args);
}
/// <summary>
/// Formats and writes a information log message with a default event id.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="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>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="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>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public static void LogError(this ILogger logger, Exception exception, string message = null, params object[] args)
{
logger.LogError(default(EventId), exception, message, args);
}
/// <summary>
/// Formats and writes a critical log message with a default event id.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="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);
}
}