Common.Logging.Serilog is an adapter that bridges the Common.Logging abstraction with Serilog, allowing you to use Serilog as the underlying logging framework in applications that rely on Common.Logging.
Install the package via NuGet Package Manager:
Install-Package Common.Logging.Serilog
Or using the .NET CLI:
dotnet add package Common.Logging.Serilog
Update your app.config
or web.config
file to use the Serilog factory adapter:
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog" />
</logging>
</common>
</configuration>
Configure Serilog and assign it to the global Log.Logger
instance:
using Serilog;
using Common.Logging;
class Program
{
static void Main(string[] args)
{
// Configure Serilog: -- Must set global Log.Logger instance
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
// Obtain a Common.Logging logger
var logger = LogManager.GetLogger<Program>();
logger.Info("Application has started.");
// Your application code here
// Ensure to flush and close Serilog at the end
Log.CloseAndFlush();
}
}
This project is licensed under the MIT License.