NLog启动服务异常

本文关键字:异常 服务 启动 NLog | 更新日期: 2023-09-27 18:03:44

我正在创建一个服务,现在是空的,只有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>

应用程序。config在configSections中有以下行,但不知道是否需要

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

知道我可能错过或忘记了什么配置吗?

NLog启动服务异常

我刚刚遇到了同样的问题。原因是我把连接字符串放在了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)。