Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog does not create log files

Serilog does not create the logfiles - and because of that (I think) nothing is going to be logged. Due to the target is should be an microservice, I'm using the CQRS pattern.

Here is my Startup.cs

private readonly ISession session;
private readonly ILogger logger;

public Startup(IConfiguration configuration)
{
  Configuration = configuration;

    var baseDirectory = Directory.GetDirectoryRoot(AppDomain.CurrentDomain.BaseDirectory);

    Log.Logger = new LoggerConfiguration()
                    .WriteTo
                    .Logger(lc => lc.Filter
                                    .ByIncludingOnly(e => e.Level == LogEventLevel.Information || e.Level == LogEventLevel.Debug)
                                    .WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/info.log")))
                    .WriteTo
                    .Logger(lc => lc.Filter
                                    .ByIncludingOnly(e => e.Level == LogEventLevel.Error)
                                    .WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/error.log")))

}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }

   loggerFactory.AddSerilog();
   ...//To shorten this example in Stackoverflow, I've excluded the standard code. But in reality they are of course there
}

Here is my Class, in which the logger will be injected by the DI:

public MaterialCommandHandler(ISession session, ILogger<MaterialCommandHandler> logger)
{
  this.session = session;
  this.logger = logger;
}

and the Handle method, which is located in the MaterialCommandHandler class, in which I want to write something into the log file

public async Task Handle(CreateMaterialCommand command)
{
  var material = new Material(command.Id, command.TechDesc, command.Description, command.MaterialClass);
  logger.LogDebug($"{DateTimeOffset.UtcNow} - {nameof(CreateMaterialCommand)} - received for {material} with {nameof(command.TechDesc)} : {command.TechDesc}.");
  await session.Add(material);
  await session.Commit();
  logger.LogDebug($"{DateTimeOffset.UtcNow} - {nameof(CreateMaterialCommand)} - received for {material} with {nameof(command.TechDesc)} : {command.TechDesc}.");
}

Anyone an idea, why the files were not be created - and nothing will be logged?

like image 878
Azeristar Avatar asked Oct 20 '25 13:10

Azeristar


1 Answers

You're missing the .CreateLogger() statement from the end of your log construction but I think the real problem is your Path.Combine statement is probably resulting in an invalid path or a path you're not expecting. Path.Combine has some annoying behavior you might not be aware of. Consider the following:

Path.Combine(@"C:\Temp", @"Logs\mylog.txt"); // C:\Temp\Logs\mylog.txt
Path.Combine(@"C:\Temp\", @"Logs\mylog.txt"); // C:\Temp\Logs\mylog.txt
Path.Combine(@"C:\Temp\", @"\Logs\mylog.txt"); // \Logs\mylog.txt
Path.Combine(@"C:\Temp", @"\Logs\mylog.txt"); // \Logs\mylog.txt
like image 66
pmcilreavy Avatar answered Oct 22 '25 04:10

pmcilreavy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!