C# log4net 不输出日志

一个新项目,直接用了一些之前的代码,突然跟踪不到日志了。检查发现了原因,特在此记录。 

log4net的配置文件log4net_config.xml 

log4net的应用错误代码:

public class Log

{

private static string DefaultName = "log";

 

static Log()

{

string path = AppDomain.CurrentDomain.BaseDirectory + @"\log4net_config.xml";

log4net.Config.XmlConfigurator.Configure(new FileInfo(path));

}

 

public static log4net.ILog GetLog(string logName)

{

log4net.ILog log = log4net.LogManager.GetLogger(logName);

return log;

}

 

public static void debug(string message)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsDebugEnabled)

log.Debug(message);

 

log = null;

}

 

public static void debug(string message, Exception ex)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsDebugEnabled)

log.Debug(message, ex);

 

log = null;

}

 

public static void error(string message)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsErrorEnabled)

log.Error(message);

 

log = null;

}

 

public static void error(string message, Exception ex)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsErrorEnabled)

log.Error(message, ex);

 

log = null;

}

 

public static void fatal(string message)

{

 

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsFatalEnabled)

log.Fatal(message);

 

log = null;

}

 

public static void info(string message)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsInfoEnabled)

log.Info(message);

 

log = null;

}

 

public static void warn(string message)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsWarnEnabled)

log.Warn(message);

 

log = null;

}

}

不输出日志的原因是因为, 默认private static string DefaultName = "log",在配置文件里面找不到对应的节点值。

正确的应用代码:

public class Log

{

private const string SError = "Error";

private const string SDebug = "Debug";

private const string DefaultName = "Info";

 

static Log()

{

var path = AppDomain.CurrentDomain.BaseDirectory + @"\log4net_config.xml";

log4net.Config.XmlConfigurator.Configure(new FileInfo(path));

}

 

public static log4net.ILog GetLog(string logName)

{

var log = log4net.LogManager.GetLogger(logName);

return log;

}

 

public static void Debug(string message)

{

var log = log4net.LogManager.GetLogger(SDebug);

if (log.IsDebugEnabled)

log.Debug(message);

}

 

public static void Debug(string message, Exception ex)

{

var log = log4net.LogManager.GetLogger(SDebug);

if (log.IsDebugEnabled)

log.Debug(message, ex);

}

 

public static void Error(string message)

{

var log = log4net.LogManager.GetLogger(SError);

if (log.IsErrorEnabled)

log.Error(message);

}

 

public static void Error(string message, Exception ex)

{

var log = log4net.LogManager.GetLogger(SError);

if (log.IsErrorEnabled)

log.Error(message, ex);

}

 

public static void Fatal(string message)

{

var log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsFatalEnabled)

log.Fatal(message);

}

 

public static void Info(string message)

{

log4net.ILog log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsInfoEnabled)

log.Info(message);

}

 

public static void Warn(string message)

{

var log = log4net.LogManager.GetLogger(DefaultName);

if (log.IsWarnEnabled)

log.Warn(message);

}

}

总结: log4net.LogManager.GetLogger(Name),这里面的Name要在配置文件中,有对应的节点值。

你可能感兴趣的:(C# log4net 不输出日志)