Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 配置Microsoft.Owin.Logging.ILogger_C#_Logging_Log4net_Owin - Fatal编程技术网

C# 配置Microsoft.Owin.Logging.ILogger

C# 配置Microsoft.Owin.Logging.ILogger,c#,logging,log4net,owin,C#,Logging,Log4net,Owin,如何配置我的ILogger以写入其他位置?我想将日志写入C:\logs\MyLogFile.log 我正在设置我的自定义LoggerFactory如下: app.Properties["server.LoggerFactory"] = new MyCustomLoggerFactory(); MyCustomLoggerFactory如下所示: public class MyCustomLoggerFactory: ILoggerFactory { public ILogger Cre

如何配置我的
ILogger
以写入其他位置?我想将日志写入
C:\logs\MyLogFile.log

我正在设置我的自定义
LoggerFactory
如下:

app.Properties["server.LoggerFactory"] = new MyCustomLoggerFactory();
MyCustomLoggerFactory
如下所示:

public class MyCustomLoggerFactory: ILoggerFactory
{
    public ILogger Create(string name)
    {
        return new CustomLogger();
    }
}
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");
    }
}
然后,
ICustomLogger
接口和
CustomLogger
类如下所示:

public class MyCustomLoggerFactory: ILoggerFactory
{
    public ILogger Create(string name)
    {
        return new CustomLogger();
    }
}
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");
    }
}
公共接口ICustomLogger:ILogger
{
}
公共类CustomLogger:ICustomLogger
{
public bool WriteCore(TraceEventType eventType、int eventId、对象状态、,
异常(函数格式化程序异常)
{
抛出新的NotImplementedException(“不要使用这一个伪”);
}
}

是否无法设置日志文件写入的日志级别或Url?理想情况下,我也希望使用log4net来实现此功能。

您可以在自定义记录器中实现log4net,您必须使用TraceEventType来确定日志记录级别:

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(...);
            }
            ....

    }
}
公共类CustomLogger:ICustomLogger
{
public bool WriteCore(TraceEventType eventType、int eventId、对象状态、,
异常(函数格式化程序异常)
{
ILog logger=LogManager.GetLogger(“myownlogger”);
开关(事件类型){
案例(TraceEventType.Critical):
{
死亡(…);
}
....
}
}
确保您有一个configure,并且您可以进行log4net配置以写入特定位置或appender