我正在使用Common.Logging.Log4Net.Universal以xml格式写入日志,以便我可以使用YALV读取它! 但是,我看不到为此生成的日志。 我已经将这种技术用于普通的log4net库,并且没有任何问题。 以下是我的'web.config'文件中的相关配置:
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Universal.Log4NetFactoryAdapter, Common.Logging.Log4Net.Universal" >
<arg key="configType" value="FILE"/>
<arg key="configFile" value="~/log4net.config" />
</factoryAdapter>
</logging>
</common>
这是我的'log4net.config'文件:
<configuration>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C://Logs//mylogfile.xml" type="log4net.Util.PatternString"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
现在,下面是实际生成日志的代码:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyNamespace
{
public class HomeController : Controller
{
private static readonly ILog Log = new Log4NetFactoryAdapter().GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public ActionResult Index()
{
Log.Error(m=>m("My First Logs"));
return View();
}
}
}
执行时,代码执行没有任何错误,但是上面提到的“mylogfile.xml”永远不会生成。
请咨询,以防有人试过。
答案 0 :(得分:2)
当我查看Universal日志项目的GitHub源代码时,我看到了web.config的以下配置:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Universal.Log4NetFactoryAdapter, Common.Logging.Log4Net.Universal" />
</logging>
</common>
</configuration>
然后你应该从&#39; log4net.config&#39;中删除配置根元素。文件:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C://Logs//mylogfile.xml" type="log4net.Util.PatternString"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
如果不起作用,请启用log4net调试以查看配置出现的问题。