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?
WebhookControllerto your program class, and you expected that to what? Work?