Serilog unable to write to file from a static class

first100 81 Reputation points
2022-09-09T08:22:44.74+00:00

Hi,

Im using serilog in my asp core 6 application, and works fine and write to a log file.

Inside my project i use static class for some processing needs, in these, i would to use serilog to log to file.
Im unable to configure in this class serilog to write to file ,it seems that it is not possible to configure the logger in some way

public static class ProcessItem  
{  
    private static readonly Serilog.ILogger _log = Log.ForContext(typeof(ProcessItem));  
    // i have log here   
     
    public static async void Process(Item item)  
    {  
        _log.Information("this is a log");  
        // this line log  but i dont know where (no exception)  
     }  

}

It does not seem possible to configure via WriteTo method where to write, as I do in other parts of my application.

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,190 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Estéfano Ferreira 1 Reputation point
    2022-12-20T02:54:49.36+00:00

    Hello,

    An alternative to using static classes is to pass the instantiation from the serialog of the parent method.

    Ex:

        public static void Process(Item item, Serilog.ILogger _log)  
        {  
            _log.Information("this is a log");  
            // this line log  but i dont know where (no exception)  
        }  
    
    0 comments No comments