NLog - MySQL数据库目标发生StackOverflow异常/ NLog.NLogRuntimeException

时间:2016-03-21 14:53:28

标签: c# mysql .net logging nlog

我已将NLog配置为将错误/日志记录到两个MySQL /数据库目标:

<nlog autoReload="false" internalLogFile="omg.txt" throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!--<nlog autoReload="false" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">-->
  <variable name="DefaultLayout" value="${longdate} | ${uppercase:${level}} | ${logger} | ${message}&#xD;&#xA;     ${exception:innerFormat=ToString:maxInnerExceptionLevel=6:innerExceptionSeparator=InnerException:format=ToString}" />
  <targets>
    <target xsi:type="ColoredConsole" name="console" layout="${DefaultLayout}" />
    <target xsi:type="Null" name="nulltarget" formatMessage="false" layout="${DefaultLayout}" />
    <target xsi:type="Database"
          name="MySqlErrorTarget"
          dbProvider="MySql.Data.MySqlClient" connectionString="x"
          keepConnection="true">
      <commandText>
        CALL Error_Insert(@Message)
      </commandText>
      <parameter name="@Message" layout="${message}" />
      (...)
    </target>
    <target xsi:type="Database"
          name="MySqlLogTarget"
          dbProvider="MySql.Data.MySqlClient" connectionString="x"
          keepConnection="true">
      <commandText>
        CALL Log_Insert(@Message)
      </commandText>
      <parameter name="@Message" layout="${message}" />
      (...)
    </target>
  </targets>
  <rules>
    <logger name="*" levels="TRACE, DEBUG, INFO" writeTo="MySqlLogTarget"/>
    <logger name="*" levels="WARN, ERROR, FATAL" writeTo="MySqlErrorTarget"/>
    <logger name="*" minlevel="INFO" writeTo="console" />
  </rules>
</nlog>

当MySQL数据库可用时,一切都很好,但是当MySQL发生故障时,NLog会抛出StackOverflow异常。内部日志文件如下所示:

2016-03-21 15:36:33.0487 Error Error when writing to database MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.GetAddrInfo(String name)
   at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
   at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
   at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at NLog.Targets.DatabaseTarget.OpenConnection(String connectionString)
   at NLog.Targets.DatabaseTarget.EnsureConnectionOpen(String connectionString)
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)
2016-03-21 15:37:00.1794 Error Error when writing to database MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.GetAddrInfo(String name)
   at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
   at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
   at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at NLog.Targets.DatabaseTarget.OpenConnection(String connectionString)
   at NLog.Targets.DatabaseTarget.EnsureConnectionOpen(String connectionString)
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)

这是无限的,因此最终会发生StackOverflow。 Visual Studio中报告的异常如下所示:

NLog.NLogRuntimeException occurred
  HResult=-2146233088
  Message=Exception occurred in NLog
  Source=NLog
  StackTrace:
       at NLog.LoggerImpl.<>c__DisplayClass1.<Write>b__0(Exception ex)
       at NLog.Internal.SingleCallContinuation.Function(Exception exception)
       at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
       at NLog.Targets.Target.WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
       at NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException)
       at NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
       at NLog.Logger.Debug(String message)
       at Runner.Main(String[] args) in C:\Repos\x\src\Runner.cs:line 36
  InnerException: 
       ErrorCode=-2147467259
       HResult=-2147467259
       Message=Unable to connect to any of the specified MySQL hosts.
       Number=1042
       Source=MySql.Data
       StackTrace:
            at MySql.Data.MySqlClient.NativeDriver.Open()
            at MySql.Data.MySqlClient.Driver.Open()
            at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
            at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
            at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
            at MySql.Data.MySqlClient.MySqlPool.GetConnection()
            at MySql.Data.MySqlClient.MySqlConnection.Open()
            at NLog.Targets.DatabaseTarget.OpenConnection(String connectionString)
            at NLog.Targets.DatabaseTarget.EnsureConnectionOpen(String connectionString)
            at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
            at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)
            at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
       InnerException: 
            ErrorCode=11001
            HResult=-2147467259
            Message=No such host is known
            NativeErrorCode=11001
            Source=System
            StackTrace:
                 at System.Net.Dns.GetAddrInfo(String name)
                 at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
                 at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
                 at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
                 at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
                 at MySql.Data.MySqlClient.NativeDriver.Open()
            InnerException: 

我已尝试将规则更改为:

  <rules>
    <logger name="NLog.*" minlevel="TRACE" writeTo="nulltarget" final="true"/>
    <logger name="MySql.*" minlevel="TRACE" writeTo="nulltarget" final="true"/>
    <logger name="System.*" minlevel="TRACE" writeTo="nulltarget" final="true"/>
    <logger name="*" levels="TRACE, DEBUG, INFO" writeTo="MySqlLogTarget"/>
    <logger name="*" levels="WARN, ERROR, FATAL" writeTo="MySqlErrorTarget"/>
    <logger name="*" minlevel="INFO" writeTo="console" />
  </rules>

然而,它没有帮助。任何想法如何避免StackOverflowException?

2 个答案:

答案 0 :(得分:0)

throwExceptions上的<nlog设置为false。 NLog将忽略连接异常,NLog不会尝试将其记录到数据库中。

答案 1 :(得分:0)

事实证明,在我的代码库中的某个地方调用了这一行:

Trace.Listeners.Add(new NLogTraceListener());

导致来自MySql的错误日志被重定向到Trace,并且由于NLogTraceListener,它被NLog使用MySql目标记录,该目标失败并被重定向到Trace,并且由于NLogTraceListener,它被NLog记录使用MySql目标,...你知道这是怎么回事。

我没有提出一个很好的解决方案,因为没有足够的信息来过滤TraceListener中的日志(只是作为字符串的消息)。因此我暂时关闭了NLogTraceListener,因为它并不重要。