2015-07-20 72 views
1

我正在提供一項服務,即現在爲空,且僅使用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"/> 

任何想法我可能錯過或忘記了什麼配置?

+0

如果你在一個單獨的文件中有你的nlog配置,那麼你不需要app.config中的configsentions。只要刪除這一行:'

' – nemesv

+0

@nemesv同樣的結果 – Kiwi

回答

5

我剛剛遇到同樣的問題。原因是我將連接字符串放在App.Config配置的頂部。恢復App.Config以使用configSections修復它。我不需要添加nlog部分。

這樣:

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

+0

謝謝你的解決方案!它也幫助了我。 –

+1

非常感謝!這很奇怪,但工作:) –

+0

如上所述,怪異!但工作。 – jeromeyers