I am having difficulty writing to a log file with Serilog. I have posted my issue on StackOverflow here but haven't been able to get to the bottom of it. Is anybody here able to help, please?
I am developing a WinForms application in .NET Core 3.0. I would like to log debug information to a file. I'd like to get a reference to my logger in various classes using dependency injection.
Most of the articles and documentation I found focused on ASP .NET rather than WinForms, so I had to use my imagination a bit when setting up Serilog in my Program's static void Main() method.
public class Program
{
private static Logger _logger; // I noticed that you don't have to do
// this in the ASP .NET samples as it's
// taken care of "under the hood".
// However, I couldn't see any
// alternative in the standard
// WinForms template in VS.
private static void Main()
{
//Omitted some code about configuration files and WinForms initialization for brevity
var services = new ServiceCollection();
ServiceProvider = ConfigureServices(services);
Application.Run(Form1);
}
public static IServiceProvider ServiceProvider { get; private set; }
private static ServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(GetLogger(), true));
GetLogger().Critical("Hello!!"); // Just testing... nothing happens. Where's my log file?
//Omitted registration of various unrelated services for brevity
return services.BuildServiceProvider();
}
private static Logger GetLogger()
{
if(_logger is null)
{
_logger = new LoggerConfiguration()
.WriteTo.File(@"c:\BenjaminLog.txt", restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug, rollingInterval: RollingInterval.Day)
.CreateLogger();
}
return _logger;
}
}
}
This code builds and runs without exception but no log file is generated on c:\
What did I do wrong?
Hi! My best guess is that your app (unless it's being run "as Administrator") won't have permission to write to the C:\ root directory. Try out SelfLog if that doesn't do the job. Cheers!
I am having difficulty writing to a log file with Serilog. I have posted my issue on StackOverflow here but haven't been able to get to the bottom of it. Is anybody here able to help, please?
I am developing a WinForms application in .NET Core 3.0. I would like to log debug information to a file. I'd like to get a reference to my logger in various classes using dependency injection.
Most of the articles and documentation I found focused on ASP .NET rather than WinForms, so I had to use my imagination a bit when setting up Serilog in my Program's static void Main() method.
This code builds and runs without exception but no log file is generated on c:\ What did I do wrong?