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