3

Logging is working but it is not posting to my local seq, I tried adding serilog and also useSerilog but it does not logging into my local service: related question

.csproj

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.1" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />

Program

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(loggingBuilder => {
                loggingBuilder.AddEventLog();
                loggingBuilder.AddSerilog();
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            })
            .UseSerilog();

appSettings.json:

"Serilog": {
"LevelSwitches": { "$controlSwitch": "Verbose" },
"MinimumLevel": { "ControlledBy": "$controlSwitch" },
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341",
      "apiKey": "yeEZyL3SMcxEKUijBjN",
      "controlLevelSwitch": "$controlSwitch"
    }
  }
]

}

Am I missing some configuration or any Nuget package? I have been successfully using seq in ASP.NET Core API, but so far cannot use it in a worker service.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
Exec21
  • 797
  • 3
  • 7
  • 23
  • Did you try enabling the [self logging as described in the documentation](https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics)? That may help identify issues. Also you should check the ingestion log in Seq. – mason Oct 08 '19 at 19:51
  • A useful article [Adding Serilog to the ASP.NET Core Generic Host](https://andrewlock.net/adding-serilog-to-the-asp-net-core-generic-host/) – Søren Jan 05 '21 at 06:14

1 Answers1

7

After spending more time figuring out, the problem was the appsettings set up and the way i was injecting the logger

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.1" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />

Host.CreateDefaultBuilder(args)
            .ConfigureLogging(loggingBuilder =>
            {
                var configuration = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json")
                    .Build();
                var logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(configuration)
                    .CreateLogger();
                loggingBuilder.AddSerilog(logger, dispose: true);
            })

Settings:

"Serilog": {
"LevelSwitches": { "$controlSwitch": "Verbose" },
"MinimumLevel": { "ControlledBy": "$controlSwitch" },
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341",
      "apiKey": "yeEZyL3SMcxEKUijBjN",
      "controlLevelSwitch": "$controlSwitch"
    }
  }
]
Exec21
  • 797
  • 3
  • 7
  • 23
  • This helped me get mine working, but I found that my underlying issue with UserSerilog was not assigning my logger to the static logger. Once I did that it started working. – Vale Trujillo Jul 12 '20 at 03:47
  • Also leaving out the `logger` parameter in `loggingBuilder.AddSerilog()` will make it quietly not do anything, so if it's not working check that. – Chad Hedgcock Aug 31 '20 at 18:18