0

I have a program that worked on this Monday (2018-10-01) but started to crash on Wednesday (it wasn't used on Tuesday).

The program is at a customer and in release form, so I can't debug it! The customer says that "Nothing has changed" and all the files in my program is as before.

It's a C# Winforms program and I have, via Event Viewer, found out that the crash happens when NLog is starting (in ctor).

I have tried to use internal logging but no log file is created.

Any ideas on what the problem is and/or how I should fix it?

The message in Event Viewer Application:

DCMark Winform.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Xml.XmlException at System.Xml.XmlTextReaderImpl.Throw(System.Exception) at System.Xml.XmlTextReaderImpl.Throw(System.String, System.String[])
at System.Xml.XmlTextReaderImpl.ParseText(Int32 ByRef, Int32 ByRef, Int32 ByRef) at System.Xml.XmlTextReaderImpl.ParseText() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Xml.XmlTextReaderImpl.Skip() at System.Xml.XmlTextReader.Skip() at System.Configuration.XmlUtil.StrictSkipToNextElement(System.Configuration.ExceptionAction) at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(System.Configuration.XmlUtil, System.String, Boolean, System.String, System.Configuration.OverrideModeSetting, Boolean) at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(System.Configuration.XmlUtil, System.String, Boolean, System.String, System.Configuration.OverrideModeSetting, Boolean) at System.Configuration.BaseConfigurationRecord.ScanSections(System.Configuration.XmlUtil) at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()

Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean) at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors) at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(System.Object, System.Configuration.Internal.InternalConfigEventArgs)

Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(System.String) at System.Configuration.ConfigurationManager.get_AppSettings() at NLog.Common.InternalLogger.GetSettingString(System.String, System.String) at NLog.Common.InternalLogger.GetSetting[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, System.String, Boolean) at NLog.Common.InternalLogger..cctor()

Exception Info: System.TypeInitializationException at DC.DCMark.Form1..ctor() at DC.DCMark.Program.Main()

The NLog.config file

<?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>

EDIT: Added the App.config file

<?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>
Andis59
  • 559
  • 7
  • 25
  • 1
    Please be aware that `throwExceptions="true"` should never be used in a production environment. It is only intended for unit-testing or similar. It has many unwanted side-effects. – Rolf Kristensen Oct 05 '18 at 22:42
  • Good to know!, I only added it while I was testing to see if internal logging would be triggered, but no... – Andis59 Oct 06 '18 at 07:31
  • Ex. It will cause NLog to crash your application when app.config is unreadable. – Rolf Kristensen Oct 06 '18 at 08:40

2 Answers2

1

There is a XML error in your web.config/app.config

In NLog 4.4 it's fixed that NLog won't crash on it. (see bug report), so updating is would fix it. Update to the latest version, 4.5.10 is advised.

Julian
  • 33,915
  • 22
  • 119
  • 174
  • I'm using a nlog.config file. The issue talks about when nlog configuration is in the app.config file. Tha program worked on monday but not today and no change has been done to the program files, including app.config and nlog.config – Andis59 Oct 05 '18 at 08:16
  • The error is pretty clear `System.Configuration.ConfigurationErrorsException` in ... `System.Configuration.ConfigurationManager.get_AppSettings()` – Julian Oct 05 '18 at 09:33
  • It could be that there is a .config changed, and that app/web.config is inheriting from that config. – Julian Oct 05 '18 at 09:35
  • I added the App.config file in the question. This is the current one and it's the same as before when the program worked – Andis59 Oct 05 '18 at 11:33
1

I have found the error!

I found this https://stackoverflow.com/a/16332304/1009355 Which says that there is a copy of app.config created in %localappdata%\companyname\programname...

I removed all directories that had the program name in its name.

Everything works again!!

Thanks for all the help, I wouldn't have found the solution without it!

Andis59
  • 559
  • 7
  • 25