简体   繁体   中英

How to implement OWIN Ilogger in ASP.NET?

I am trying to just do some simple logging and write trace information about errors that may occur in an web application. Here is some example code:

public class NewLogger : Ilogger
{

}

I maybe missing some understanding of interfaces, so if I can get a brief explanation how to implement this interface so that I could write a trace that would be helpful!

I get an error like below:

Error 1 'NewLogger' does not implement interface member 'Microsoft.Owin.Logging.ILogger.WriteCore(System.Diagnostics.TraceEventType, int, object, System.Exception, System.Func)'

I have tried to implement this though am unable to find the values I need to use the method WriteCore, which I believe I need.

You need to make an implementation for the WriteCore method on your NewLogger class (like the error message explains). Something like this:

public class NewLogger : ILogger
{
    public bool WriteCore(TraceEventType eventType, int eventId, object state,
        Exception exception, Func<object, Exception, string> formatter)
    {
        if (eventType == TraceEventType.Critical)
        {
            Debug.Fail("We have a critical error " + exception);
        }
        //etc for other eventtypes

        return true;
    }
}

However, I think you should not make a custom logger in your case and rather use existing implementations, like this one for NLog: http://k16c.eu/2014/12/27/using-nlog-owin-logger/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM