我一直在努力在我的ASP.NET MVC 3应用程序上设置NLog v2,迄今为止已经运行得很好。 (我正在使用官方nuGet存储库中的包)但是,当我尝试更改日志布局以包含任何aspnet- *布局渲染器时,我得到一个配置错误。我将问题减少到以下最低用例:
在configSections块中:
<section name="nlog" type="NLog.Config.ConfigSectionHandler,NLog"/>
Nlog块:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> <variable name="logDirectory" value="C:\Logs" /> <targets> <target name="logFile" xsi:type="File" fileName="${logDirectory}\app.log" layout="${aspnet-user-identity}" /> </targets> <rules> <logger name="*" minlevel="Info" writeTo="logfile" /> </rules>
如果我改变布局,使用不属于aspnet *系列的任何渲染器的组合,这样做很好(我没有测试过,但是我已经看了很多)。我得到的错误在这里:
Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: An error occurred creating the configuration section handler for nlog: Exception occurred when loading configuration from C:\..[snip]..\web.config Source Error: Line 16: </configSections> Line 17: Line 18: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" Line 19: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> Line 20:
我真的不知道发生了什么。我不知道该渲染器会导致配置无效。我一直在大声喧哗,没有任何地方,所以我希望有人能帮忙。
谢谢!
解决方法
请确保您引用了NLog.Extended程序集,这些程序集定义了哪些布局,哪些必须由NuGet软件包添加到引用中:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> <extensions> <add assembly="NLog.Extended" /> </extensions> <variable name="logDirectory" value="C:\Logs" /> <targets> <target name="logFile" xsi:type="File" fileName="${logDirectory}\app.log" layout="${aspnet-user-identity} ${message}" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="logfile" /> </rules> </nlog>