如何配置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();
}
}
然后ICustomLogger
接口和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");
}
}
是否无法设置日志文件写入的LogLevel或Url?理想情况下,我也想使用log4net。
答案 0 :(得分:3)
您可以在自定义记录器中创建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(...);
}
....
}
}
确保您有一个配置,您可以使用log4net配置写入特定位置或追加器。