Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Microsoft.Owin.Logging.ILogger

How do I configure my ILogger to write to a different location? I would like to write my logs to C:\Logs\MyLogFile.log.

I am setting my custom LoggerFactory like so:

app.Properties["server.LoggerFactory"] = new MyCustomLoggerFactory();

MyCustomLoggerFactory looks like this:

public class MyCustomLoggerFactory: ILoggerFactory
{
    public ILogger Create(string name)
    {
        return new CustomLogger();
    }
}

Then ICustomLogger interface and CustomLogger class look like this:

public interface ICustomLogger : ILogger
{

}

public class CustomLogger : ICustomLogger 
{
    public bool WriteCore(TraceEventType eventType, int eventId, object state, 
        Exception exception, Func<object, Exception, string> formatter)
    {
        throw new NotImplementedException("Dont use this one dummy");
    }
}

Is there no way to set the LogLevel or Url where the log file gets written? Ideally I would like to also use log4net for this.

like image 375
gwin003 Avatar asked Oct 20 '22 18:10

gwin003


1 Answers

You can make a log4net implementation for in your custom logger, you have to use the TraceEventType to determine the logging level:

public class CustomLogger : ICustomLogger 
{
    public bool WriteCore(TraceEventType eventType, int eventId, object state, 
        Exception exception, Func<object, Exception, string> formatter)
    {
        ILog logger = LogManager.GetLogger("myowinlogger");
        switch (eventType){
            case(TraceEventType.Critical): 
            {
                logger.Fatal(...);
            }
            ....

    }
}

Make sure you have a configure and you can make a log4net configuration to write to a specific location or appender.

like image 121
Peter Avatar answered Oct 27 '22 00:10

Peter