datalust / seq-extensions-logging

Add centralized log collection to ASP.NET Core apps with one line of code.
https://datalust.co/seq
Apache License 2.0
86 stars 13 forks source link

Logging to Seq using ILogger interface on a console application #44

Closed omidkrad closed 3 years ago

omidkrad commented 3 years ago

Hi, the documentations shows how to set this up on a web app using special hooks provided by ASP.NET, but how can I set it up on a simple console app?

Thanks!

nblumhardt commented 3 years ago

Hi! Calling AddSeq() on the logging builder using this pattern should work:

https://docs.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#non-host-console-app

Hope this helps!

omidkrad commented 3 years ago

Yes it worked, thanks. For some reason I also needed to add Microsoft.Extensions.Logging.Console package for LoggerFactory.Create method to work even if I don't add console logger.

nblumhardt commented 3 years ago

That's great - thanks for the follow-up 👍

djeikyb commented 2 years ago

FYI for anyone else having problems using a generic host in a console app, it's critical to using the host:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using var host = Host.CreateDefaultBuilder()
    .ConfigureLogging(builder => builder
        .AddConsole()
        .AddSeq())
    .Build();

await host.StartAsync();

var logger = host.Services.GetRequiredService<ILogger<Program>>();

logger.LogInformation("Hello, {Name}!", Environment.UserName);

My only change to the console example (many thanks datalust team for the example project!) is adding a dependency on the generic host packge:

<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />

The using flushes any queued up log messages to Seq.