【问题标题】:NLog 4.1.2 crashes in constructorNLog 4.1.2 在构造函数中崩溃
【发布时间】:2018-10-05 07:29:30
【问题描述】:

我有一个程序在这个星期一(2018-10-01)工作,但在星期三开始崩溃(星期二没有使用)。

该程序在客户处并处于发布形式,所以我无法调试它! 客户说“什么都没有改变”,我程序中的所有文件都和以前一样。

这是一个 C# Winforms 程序,我通过事件查看器发现崩溃发生在 NLog 启动时(在 ctor 中)。

我尝试使用内部日志记录,但没有创建日志文件。

关于问题是什么和/或我应该如何解决它的任何想法?

事件查看器应用程序中的消息:

DCMark Winform.exe 框架 版本:v4.0.30319 描述:进程因 未处理的异常。异常信息:System.Xml.XmlException 在 System.Xml.XmlTextReaderImpl.Throw(System.Exception) 在 System.Xml.XmlTextReaderImpl.Throw(System.String, System.String[])
在 System.Xml.XmlTextReaderImpl.ParseText(Int32 ByRef,Int32 ByRef, Int32 ByRef) 在 System.Xml.XmlTextReaderImpl.ParseText() 在 System.Xml.XmlTextReaderImpl.ParseElementContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlTextReader.Read() 在 System.Xml.XmlTextReaderImpl.Skip() 在 System.Xml.XmlTextReader.Skip() 在 System.Configuration.XmlUtil.StrictSkipToNextElement(System.Configuration.ExceptionAction) 在 System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(System.Configuration.XmlUtil, System.String,布尔,System.String, System.Configuration.OverrideModeSetting,布尔值)在 System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(System.Configuration.XmlUtil, System.String,布尔,System.String, System.Configuration.OverrideModeSetting,布尔值)在 System.Configuration.BaseConfigurationRecord.ScanSections(System.Configuration.XmlUtil) 在 System.Configuration.BaseConfigurationRecord.InitConfigFromFile()

异常信息:System.Configuration.ConfigurationErrorsException
在 System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(布尔值) 在 System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors) 在 System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
在 System.Configuration.ClientConfigurationSystem.OnConfigRemoved(System.Object, System.Configuration.Internal.InternalConfigEventArgs)

异常信息:System.Configuration.ConfigurationErrorsException
在 System.Configuration.ConfigurationManager.PrepareConfigSystem()
在 System.Configuration.ConfigurationManager.GetSection(System.String) 在 System.Configuration.ConfigurationManager.get_AppSettings() 在 NLog.Common.InternalLogger.GetSettingString(System.String, System.String) 在 NLog.Common.InternalLogger.GetSetting[[System.Boolean, mscorlib, 版本=4.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089]](System.String, System.String, Boolean) 在 NLog.Common.InternalLogger..cctor()

异常信息:System.TypeInitializationException 在 DC.DCMark.Program.Main() 中的 DC.DCMark.Form1..ctor()

NLog.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<nlog throwExceptions="true"
internalLogFile="C:/Temp/log.txt" internalLogLevel="Trace">
  <targets>
    <target xsi:type="File" name="f" fileName="${specialfolder:folder=CommonApplicationData}/Foo/Bar/Logs/Bar_${shortdate}.log"
            layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message} | ${exception:format=ToString}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="f" />
  </rules>
</nlog>

编辑:添加 App.config 文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="DC.DCMark.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    </startup>
    <userSettings>
        <DC.DCMark.Properties.Settings>
            <setting name="Location" serializeAs="String">
                <value>400, 100</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>840, 900</value>
            </setting>
            <setting name="Maximised" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Minimised" serializeAs="String">
                <value>False</value>
            </setting>
        </DC.DCMark.Properties.Settings>
    </userSettings>
</configuration>

【问题讨论】:

  • 请注意,throwExceptions="true" 绝不应该在生产环境中使用。它仅用于单元测试或类似的。它有许多不需要的副作用。
  • 很高兴知道!我只是在测试时添加它以查看是否会触发内部日志记录,但不会...
  • 前。当 app.config 不可读时,它会导致 NLog 使您的应用程序崩溃。

标签: c# nlog


【解决方案1】:

您的 web.config/app.config 中存在 XML 错误

在 NLog 4.4 中修复了 NLog 不会崩溃的问题。 (请参阅bug report),因此更新将修复它。更新到最新版本,建议 4.5.10。

【讨论】:

  • 我正在使用一个 nlog.config 文件。该问题涉及 nlog 配置何时位于 app.config 文件中。 Tha 程序周一运行,但今天不行,程序文件没有做任何更改,包括 app.config 和 nlog.config
  • 错误很明显System.Configuration.ConfigurationErrorsException in ... System.Configuration.ConfigurationManager.get_AppSettings()
  • 可能是 .config 更改了,并且 app/web.config 继承自该配置。
  • 我在问题中添加了 App.config 文件。这是当前的,与之前程序运行时相同
【解决方案2】:

我发现了错误!

我找到了这个https://stackoverflow.com/a/16332304/1009355 这表示在 %localappdata%\companyname\programname... 中创建了 app.config 的副本...

我删除了名称中包含程序名称的所有目录。

一切都恢复正常了!!

感谢所有帮助,如果没有它,我将无法找到解决方案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多