2

I am trying to log with Serilog in an ASP.NET Core application's Program.cs class. My current setup is as follows:

public class Program
{
    public static void Main(string[] args)
    {
        DoStuff();
        CreateWebHostBuilder(args).Build().Run();
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, builder) =>
            {
                builder.AddFile(@"C:\...\log-{Date}.txt", isJson: true);
            })
            .UseStartup<Startup>();
}

I can successfully log in a Controller class by doing:

public class WebhookController : Controller
{
    readonly ILogger<WebhookController> _log;
    public WebhookController(ILogger<WebhookController> log)
    {
        _log = log;
    }
    public IActionResult Index()
    {
        _log.LogInformation("hello world");
        return View();
    }
}

However, when trying to replicate this in the program class, _log is not accessible in Main(string[] args):

public class Program
{
    readonly ILogger<Program> _log;
    public Program(ILogger<Program> log)
    {
        _log = log;
    }

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, builder) =>
            {
                builder.AddFile(@"C:\...\log-{Date}.txt", isJson: true);
            })
            .UseStartup<Startup>();
}

How can I configure Serilog to log in the program class?

4
  • You added a constructor named WebhookController to your program class, and you expected that to what? Work? Commented Dec 23, 2018 at 15:06
  • Thanks for bringing that to my attention. No, I didn't actually do that, it's rather late where I am and I must've copy and pasted without double checking! Commented Dec 23, 2018 at 15:11
  • Got it! Thanks for clarifying. See this for some ideas on how to set up Serilog in .NET Core. Commented Dec 23, 2018 at 15:27
  • @HarryStuart Has your problem been solved? Commented Dec 23, 2018 at 16:15

2 Answers 2

3

When the web host is built, the IWebHost exposes IWebHost.Services Property

public IServiceProvider Services { get; }

which can be used to resolve the desired dependencies.

public class Program {

    public static void Main(string[] args) {
        var webHost = CreateWebHostBuilder(args).Build();
        var services = webHost.Services;

        var log = services.GetService<ILogger<Program>>();

        DoStuff(log);

        webHost.Run();
    }

    static void DoStuff(ILogger<Program> log) {
        //...
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, builder) =>
            {
                builder.AddFile(@"C:\...\log-{Date}.txt", isJson: true);
            })
            .UseStartup<Startup>();
}

Remember to include using Microsoft.Extensions.DependencyInjection; in order to have access to the .GetService<T>() extension method on IServiceProvider.

Sign up to request clarification or add additional context in comments.

Comments

0

try adding Program to your services. something like this:

.Configure(services => services.AddTransiant<Program>())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.