启动服务上的NLog异常

时间:2015-07-20 07:32:00

标签: c# windows-services nlog

我正在提供服务,现在是空的,只使用NLog,但是当我启动服务时,我收到以下错误;

---------------------------
Services
---------------------------
Windows could not start the JobService service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion.

---------------------------
OK   
---------------------------

这是事件查看器中的堆栈跟踪:

Application: service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
Stack:
   at System.Configuration.ClientConfigurationSystem.EnsureInit(System.String)
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(System.String)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(System.String)
   at System.Configuration.ConfigurationManager.GetSection(System.String)
   at NLog.Config.XmlLoggingConfiguration.get_AppConfig()
   at NLog.LogFactory.get_Configuration()
   at NLog.LogFactory.GetLogger(LoggerCacheKey)
   at NLog.LogFactory.GetLogger(System.String)
   at NLog.LogManager.GetLogger(System.String)
   at JobsService.Service1..ctor()

NLog.config:

<?xml version="1.0" encoding="utf-8" ?>
<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" >

  <targets>
    <target name="file" xsi:type="File" layout="${longdate} ${logger} ${message}" fileName="${basedir}/${shortdate}.log" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>

App.config在configSections中有以下行,但不知道是否需要

<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>

知道我可能错过了什么配置,或者忘了?

1 个答案:

答案 0 :(得分:5)

我刚刚遇到同样的问题。原因是我将连接字符串放在App.Config中的配置顶部。恢复App.Config以启动configSections修复它。我不需要添加nlog-section。

因此:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add ... />
  </connectionStrings>
  <!-- etcetera -->
</configuration>

不确定为什么会这样。

注意,我在Windows服务中使用NuGet的最新NLOG(v4)。