简体   繁体   English

如何在ASP.NET中实现OWIN Ilogger?

[英]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. 我试图做一些简单的日志记录,并编写有关Web应用程序中可能发生的错误的跟踪信息。 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)' 错误1'NewLogger'未实现接口成员'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. 尽管无法找到使用方法WriteCore所需的值,但我已经尝试实现了这一点,我认为我需要。

You need to make an implementation for the WriteCore method on your NewLogger class (like the error message explains). 您需要在NewLogger类上实现WriteCore方法的实现(如错误消息所述)。 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/ 但是,我认为您不应该根据自己的情况创建自定义记录器,而应使用现有的实现,例如针对NLog的实现: http : //k16c.eu/2014/12/27/using-nlog-owin-logger/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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