请参阅log4net
配置(App.config
文件):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ColoredConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] [%level] [%logger/%method] --> %message%newline%exception"/>
</layout>
<mapping>
<level value="DEBUG" />
<foreColor value="Cyan, HighIntensity" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Green, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\log.txt"/>
<rollingStyle value="Date" />
<appendToFile value="true" />
<rollingStyle value="Size"/>
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="5"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] [%level] - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="Info" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
LogHelper
public class LogHelper
{
public static log4net.ILog GetLogger([CallerFilePath]string fileName = "")
{
return log4net.LogManager.GetLogger(fileName);
}
}
主要
class Program
{
private static readonly log4net.ILog log = LogHelper.GetLogger("Main");
static void Main(string[] args)
{
log.Info("Bla bla");
Console.ReadLine();
}
}
那很奇怪,我只是看不到我的log
答案 0 :(得分:2)
您需要在日志帮助器中的某个地方调用log4net.Config.XmlConfigurator.Configure();
。