Skip to content

DRNJ

Light at the end of the Technology Tunnel

  • Home
  • About
  • Contact
DRNJ

Month: September 2022

Serilog in .Net Core 6

September 25, 2022

The Problem

I wanted to add Serilog to a WebAPI using .Net Core 6. The “startup” has changed from .Net Core 3 and the “wiring” up of services so it wasn’t clear to me how to get it all working.

The Solution

After some head scratching and searching I work out that you needed from this article and this article (thanks to the authers)

So I created a method

protected void ConfigureLogging(WebApplicationBuilder appBuilder)
{
appBuilder.Host.UseSerilog((ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration)
.WriteTo.Console()
);

}

This method is called from the program.cs “startup” code.

Placed the following in appsetttings.json

{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"DRNJ": "Debug",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@mt = 'An unhandled exception has occurred while executing the request.'"
}
}
],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": "c:\\temp\\log\\apilog-.txt",
"rollingInterval": "Day"
}
}
]
},
"AllowedHosts": "*"
}

 

And, voila, it all worked

 

.NET Core

Serilog And .Net Core 3.1

September 25, 2022

The Problem

I found configuring Serilog for .Net Core quite complex and poorly documented. All I wanted to do was to pipe log messages to a text file.

The Solution

After some searching I found this project.

The following code was added to Program.cs

public static void Main(string[] args)
{ 
CreateHostBuilderWithSimpleSerilog(args).Build().Run();
}

public static IHostBuilder CreateHostBuilderWithSimpleSerilog(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
//----------------------------------------------------------------------
// Simple SeriLog |
// Using https://github.com/serilog/serilog-extensions-logging-file |
//----------------------------------------------------------------------
builder.AddFile(context.Configuration.GetLoggerConfiguration());
})
.ConfigureWebHostDefaults(webBuilder =>
{
if (bool.Parse(Environment.GetEnvironmentVariable("USE_HTTP_SYS") ?? "false"))
{
webBuilder.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.None;
options.Authentication.AllowAnonymous = true;
});
}
webBuilder.UseStartup<Startup>();
});

Along with

 public static class StartupConfiguration
    {

        public static IConfigurationSection GetLoggerConfiguration(this IConfiguration config)
        {
            return config.GetSection("Logging");
        }

        public static T Get<T>(this IConfiguration config, string name)
        {
            return config
                 .GetSection(name)
                 .Get<T>();
        }
    }

And then the Appsettings.json section for logging

 

"Logging": {
"OutputTemplate": "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} Properties: {Properties:j} ({EventId:x8}){NewLine}{Exception}",
"PathFormat": "Logs/MyLog-{Date}.log",
"MinimumLevel": "Information",
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
}
}

 

And Voila. Logging to file works and Dependency Injection of Serilog is wired up too.

.NET Core

Recent Posts

  • AutoMapper and “Could not load type ‘SqlGuidCaster'” Error
  • OpenVPN on Docker and the Strange Error Message Saga
  • Docker CLI and Compose Information Message
  • Docker Containers and Azure – An Introduction
  • Serilog in .Net Core 6

Recent Comments

    Archives

    • April 2025
    • December 2024
    • April 2024
    • September 2022
    • November 2021
    • June 2021
    • March 2021
    • July 2020
    • April 2020
    • November 2019
    • September 2019
    • July 2019
    • May 2019
    • February 2019
    • July 2018
    • June 2018

    Categories

    • .NET Core
    • Azure
    • Docker
    • DotNet
    • Security
    • Uncategorized
    • WebAPI
    • Windows

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Idealist by NewMediaThemes