Standardised logging infrastructure module for olliebear projects
To load the Serilog logging module via DependencyInjection, add the following to the ServicesCollection registration:
var services = new ServiceCollection();
...
Services.AddSerilogLogging();
"LoggingConfigurationOptions": {
"ApplicationName": "Test.Application",
"ConsoleMinimumLogLevel": "Information",
"LoggingFileConfigurations": [{
"NumberOfFilesRetained": 10,
"FileSizeLimitBytes": 450000,
"FilePath": "F:\\Logs\\",
"Format": "[{Application}],{Level},{Message}",
"MinimumLogLevel": "Verbose"
}]
}
Log level must be one of the following:
- Verbose
- Debug
- Information
- Warning
- Error
- Fatal
Strings other than these entries will throw exceptions.
{
"LoggingConfigurationOptions": {
"ApplicationName": "Infrastructure.Logging.Sample.Host",
"ConsoleMinimumLogLevel": "Debug",
"LoggingFileConfigurations": [
{
"NumberOfFilesRetained": 15,
"MinimumLogLevel": "Information",
"FilePath": "D:\\Logs\\SpecificFolder1\\"
},
{
"MinimumLogLevel": "Fatal",
"FilePath": "D:\\Logs\\SpecificFolder2"
},
{
"NumberOfFilesRetained": 1
}
]
}
}
The script to add a Logs table is located here: olliebear.infrastructure.logging\src\Infrastructure.Logging.Serilog\Script\CREATE_TABLE_LOGS.sql.
Apply this script to the target database.
{
"LoggingConfigurationOptions": {
"ApplicationName": "Infrastructure.Logging.Sample.Host",
"ConsoleMinimumLogLevel": "Debug",
"LoggingFileConfigurations": [
{
"NumberOfFilesRetained": 15,
"MinimumLogLevel": "Information",
"FilePath": "D:\\Logs\\SpecificFolder1\\"
},
{
"MinimumLogLevel": "Fatal",
"FilePath": "D:\\Logs\\SpecificFolder2"
},
{
"NumberOfFilesRetained": 1
}
],
"LoggingDatabaseConfigurations": [
{
"MinimumLogLevel": "Debug",
"ConnectionString": "Data Source=.;Initial Catalog=olliebearSCHEDULE;Integrated Security=true;",
"LoggingAdditionalColumns": [
// LoggingAdditionalColumns - allows for custom columns in the log table. PLEASE remember to add the colum with correct type and length to the Logs table
{
"ColumnName": "JobName",
"PropertyName": "JobName",
"SqlDbType": "NVarChar", // 12 = Nvarchar
"DataLength": 150
}
]
}
]
}
}
LoggingAdditionalColumns - this wi
private readonly ILog _logger;
private readonly ILogContext _logContext;
public JobRun(ILog logger, ILogContext logContext)
{
_logger = logger;
_logContext = logContext;
}
... more code ....
// the below will append all _logger calls with a property named JobName with the value of ThisIsAJob
using (_logContext.PushProperty("JobName", "ThisIsAJob"))
{
_logger.Verbose("Verbose log entry {0}", i);
_logger.Debug("Debug log entry {0}", i);
_logger.Info("Info log entry {0}", i);
_logger.Warning("Warning log entry {0}", i);
_logger.Error("Error log entry {0}", i);
_logger.Fatal("Fatal log entry {0}", i);
};
You can disable the other configured logging in your projects by adding the following to your Program / Startup code:
WebHost.CreateDefaultBuilder(args)
...
.ConfigureLogging(l => l.ClearProviders())