我在 Visual Studio Express 2013 的控制台应用程序中使用 nLog 4.0.1。
在 VS 中运行应用程序本身就可以工作;在 bin\Debug
或 bin\Release
等相对目录中编译 .exe 后,它也可以工作。
但是,如果我将其编译到另一个目录(\\192.168.3.55\vol3\programs\test
)中,它就不起作用。
这是我的应用程序配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- ... -->
</assemblyBinding>
</runtime>
<connectionStrings>
<!-- ... -->
</connectionStrings>
<appSettings>
<!-- ... -->
</appSettings>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="startDatetime" value="${date:format=yyMMdd}" />
<targets>
<target name="logfile" xsi:type="File" fileName="\\192.168.3.71\log\test_${startDatetime}.dbg" layout="[${date:format=yyyyMMddHHmmssfff}][${level}] ${message}" />layout="${date:format=yyyyMMddHHmmss} ${message}" />-->
<target xsi:type="ColoredConsole" name="console" encoding="utf-8" layout="${message}">
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>
</configuration>
这是我的代码中发生异常的部分:
Imports NLog
Module Test
Public logger As Logger = LogManager.GetCurrentClassLogger()
'...
End Module
这是我的 .vbproj 的一部分:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<Optimize>true</Optimize>
<OutputPath>\\192.168.3.55\vol3\programs\test\</OutputPath>
<DocumentationFile>test.xml</DocumentationFile>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
这是 Stacktrace(抱歉,是德语):
Unbehandelte Ausnahme: System.TypeInitializationException: Der Typeninitialisierer für "test.Test" hat eine Ausnahme verursacht. ---> System.Configuration.ConfigurationErrorsException: Beim Erstellen des Konfigurationsabschnittshandlers für "nlog" ist ein Fehler aufgetreten.: Der Typeninitialisierer für "NLog.Config.Configuration
ItemFactory" hat eine Ausnahme verursacht. (\\192.168.3.55\vol3\programs\test\test.exe.Config line 36) ---> System.TypeInitializationException: Der Typeninitialisierer für "NLog.Config.ConfigurationItemFactory" hat eine Ausnahme verursacht. ---> System.IO.DirectoryNotFoundException: Ein Teil des Pfades "\\192.168.3.55\vol3\vol3\programs\test" konnte nicht gefunden werden.
bei System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
bei System.IO.FileSystemEnumerableIterator`1.CommonInit()
bei System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
bei System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption, Boolean checkHost)
bei System.IO.Directory.InternalGetFiles(String path, String searchPattern, SearchOption searchOption)
bei NLog.Config.ConfigurationItemFactory.BuildDefaultFactory()
bei NLog.Config.ConfigurationItemFactory..cctor()
--- Ende der internen Ausnahmestapelüberwachung ---
bei NLog.Config.ConfigurationItemFactory.get_Default()
bei NLog.Config.XmlLoggingConfiguration..ctor(XmlElement element, String fileName)
bei NLog.Config.ConfigSectionHandler.Create(XmlNode section, IAppDomain appDomain)
bei System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXml
Reader reader)
bei System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object pa
rentConfig, ConfigXmlReader reader)
bei System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
bei System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)
--- Ende der internen Ausnahmestapelüberwachung ---
bei System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTruste
d, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
bei System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionReco
rd, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
bei System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
bei System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
bei System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
bei System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
bei System.Configuration.ConfigurationManager.GetSection(String sectionName)
bei NLog.LogFactory.get_Configuration()
bei NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
bei NLog.LogManager.GetCurrentClassLogger()
意思是,部分路径\\192.168.3.55\vol3\vol3\programs\test
无法找到,实际上不存在。但我不知道这是怎么发生的,“vol3”被拿了两次。我在设置中使用 \\192.168.3.55\vol3\programs\test\
,但是,在这个目录中释放后,nLog 将路径变成 \\192.168.3.55\vol3\vol3\programs\test
并失败。
我怎样才能告诉nLog使用正确的路径?
最佳答案
这是一个confirmed bug in 4.0.1 (不是 4.0.0)。这个问题将在 4.1.0 中得到解决。
关于vb.net - 如何基于 Nlog 假设路径错误修复 TypeInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650889/