A library commonly used for log standardization, aimed at enhancing flow tracking, it's is a ILogger extension.
LogTemplate can be installed using the Nuget package manager or the dotnet
CLI.
dotnet add package LogTemplate --version 6.0.1
var builder = WebApplication.CreateBuilder(args);
// Inject context
builder.Services.AddLogTemplate();
var app = builder.Build();
app.MapControllers();
// Set parameters
app.UseLogTemplate("your-application-name", "x-your-correlation-identifier");
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
try
{
_logger.StartLogInformation(new Template
{
Title = "Started Process",
Description = "No details"
});
var weatherForecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
_logger.StartLogInformation(new Template
{
Title = "Finished Process",
Description = "No details"
});
return weatherForecasts;
}
catch (Exception ex)
{
_logger.EndLogError(new TemplateError
{
Title = "Processamento finalizado",
Description = "Sem detalhe",
MessageErro = ex.Message,
StackTrace = ex.StackTrace
});
throw;
}
}
info: LogTemplateApplication.Controllers.WeatherForecastController[0]
{
"Title": "Started Process",
"Description": "No details",
"Status": "Started",
"Application": "LogTemplate",
"CorrelationId": "7d38107e-48e6-4f17-b653-3e71654a0c45",
"LogLevel": "Information"
}
info: LogTemplateApplication.Controllers.WeatherForecastController[0]
{
"Title": "Finished Process",
"Description": "No details",
"Status": "Started",
"Application": "LogTemplate",
"CorrelationId": "7d38107e-48e6-4f17-b653-3e71654a0c45",
"LogLevel": "Information"
}
LogTemplateApplication.Controllers.WeatherForecastController[0]
{
"Title": "Started Process",
"Description": "No details",
"Status": "Started",
"Application": "LogTemplate",
"CorrelationId": "7d38107e-48e6-4f17-b653-3e71654a0c45",
"LogLevel": "Information"
}
fail: LogTemplateApplication.Controllers.WeatherForecastController[0]
{
"MessageErro": "Intentional error",
"StackTrace": "at LogTemplateApplication.Controllers.WeatherForecastController.GetWithError() in LogTemplateApplication\\Controllers\\WeatherForecastController.cs:line 73",
"Title": "Ended Process",
"Description": "No details",
"Status": "Finished",
"Application": "LogTemplate",
"CorrelationId": "7d38107e-48e6-4f17-b653-3e71654a0c45",
"LogLevel": "Error"
}
Carlos Alves Linkedin profile