log4net AdoNetAppender停止记录 - 无法找到类型 - 自定义记录器

时间:2015-12-23 17:07:51

标签: c# log4net

我希望有人可以让我深入了解为什么在使用log4net的AdoNetAppender时,我会获得一天的日志,也许两天,然后他们就会停止。我将reconnectOnError设置为true,但仍然没有日志。我的bufferSize也设置为1,所以如果我写一个日志,我应该立即看到它,这是我在应用程序启动时所做的。我有两个日志表,一个用于跟踪信息,另一个用于普通日志。正常的log4net日志是未修改的,我相信总是记录,另一个是我用来跟踪Web服务请求的自定义日志。两个日志在启动后都按预期工作。

后一天,有时更多,有时更少,我回去检查日志和应用程序工作,但没有任何内容记录到自定义记录器。

更新!!让所有事情都放在一夜之后,我会在错误日志中找到它:

log4net:ERROR Failed to find type [Fcn.Logging.TrackingLayoutPattern]
System.TypeLoadException: Could not load type [Fcn.Logging.TrackingLayoutPattern]. Tried assembly [log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(XmlElement element, Type defaultTargetType, Type typeConstraint)
log4net:ERROR Failed to create object to set param: layout

所有Fcn.Logging对象都在一个单独构建的库中,名为Common,由Fcn上的多个应用程序共享。当对象是每个Web服务的一部分时,我不记得这个问题,但我相信当我将它们拆分时问题就开始了,所以我不必将它们放到我创建的每个应用程序中需要它们。

我使用以下代码初始化Global.asax.cs中的log4net:

private static readonly ILog log4net = LogManager.GetLogger(typeof(Global));
    protected void Application_Start(object sender, EventArgs e)
    {
        XmlConfigurator.Configure();
        log4net.Info("Logging initialized");
    }

我的web.config配置如下:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="E:\PseudoFileServer\Logging\wsFCNCheck\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>
<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="TestLog2.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <param name="ImmediateFlush" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c:%L] - %m%n" />
    </layout>
  </appender>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <appender name="AdoLog4NetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=Server;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=UserName;Password=UserPass" />
    <reconnectOnError>true</reconnectOnError>
    <commandText value="[WS].[WebServiceLogOnly_Insert]" />
    <commandType value="StoredProcedure" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime2" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@thread" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@exception" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>
  <appender name="AdoNetTrackingAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=Server;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=UserName;Password=UserPass" />
    <reconnectOnError>true</reconnectOnError>
    <commandText value="[WS].[WebServiceTracking_Insert]" />
    <commandType value="StoredProcedure" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime2" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@WebServiceRequestTypeKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{webservicerequesttypekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@CallerSource" />
      <dbType value="String" />
      <size value="5" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{callersource}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@UserID" />
      <dbType value="String" />
      <size value="50" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{userid}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Request" />
      <dbType value="Xml" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{request}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ResponseTypeKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{responsetypekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@WebServiceResponseMessageKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{webserviceresponsemessagekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Response" />
      <dbType value="Xml" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{response}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@StartTime" />
      <dbType value="DateTime2" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{starttime}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@EndTime" />
      <dbType value="DateTime2" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{endtime}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@StoredProcedureCallTimeInMS" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{storedprocedurecalltimeinms}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@MSGeocode" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{msgeocode}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeFailOverUsed" />
      <dbType value="Boolean" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{geocodefailoverused}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeRequest" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{geocoderequest}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeResponse" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="Fcn.Logging.TrackingLayoutPattern">
        <conversionPattern value="%Tracking{geocoderesponse}" />
      </layout>
    </parameter>
  </appender>
  <root>
    <level value="WARNING" />
    <appender-ref ref="AdoLog4NetAppender" />
  </root>
  <logger additivity="false" name="Tracking">
    <level value="INFO"/>
    <appender-ref ref="AdoNetTrackingAppender" />
  </logger>
</log4net>

我将重新连接标记更改为如下所示:

<reconnectOnError value="true" /> <!-- I believe this is the correct syntax -->
<reconnectOnError>true</reconnectOnError> <!-- This I believe is incorrect syntax -->

有人可以确认哪种语法正确吗?

此时我还更改了自定义记录器以指定它们所在的程序集。这可能是唯一的问题,但此时我收到了额外的错误。我将详细介绍配置更改,然后详细说明新错误。

<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="TestLog2.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <param name="ImmediateFlush" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c:%L] - %m%n" />
    </layout>
  </appender>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <appender name="AdoLog4NetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=Username;Password=Password" />
    <reconnectOnError value="true" />
    <commandText value="[WS].[WebServiceLogOnly_Insert]" />
    <commandType value="StoredProcedure" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime2" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@thread" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%thread" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@exception" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>
  <appender name="AdoNetTrackingAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=Username;Password=Password" />
    <reconnectOnError value="true" />
    <commandText value="[WS].[WebServiceTracking_Insert]" />
    <commandType value="StoredProcedure" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime2" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@WebServiceRequestTypeKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{webservicerequesttypekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@CallerSource" />
      <dbType value="String" />
      <size value="5" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{callersource}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@UserID" />
      <dbType value="String" />
      <size value="50" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{userid}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Request" />
      <dbType value="Xml" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{request}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ResponseTypeKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{responsetypekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@WebServiceResponseMessageKey" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{webserviceresponsemessagekey}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@Response" />
      <dbType value="Xml" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{response}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@StartTime" />
      <dbType value="DateTime2" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{starttime}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@EndTime" />
      <dbType value="DateTime2" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{endtime}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@StoredProcedureCallTimeInMS" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{storedprocedurecalltimeinms}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@MSGeocode" />
      <dbType value="Int32" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{msgeocode}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeFailOverUsed" />
      <dbType value="Boolean" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{geocodefailoverused}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeRequest" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{geocoderequest}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@GeocodeResponse" />
      <dbType value="String" />
      <size value="-1" />
      <layout type="Fcn.Logging.TrackingLayoutPattern, Common">
        <conversionPattern value="%Tracking{geocoderesponse}" />
      </layout>
    </parameter>
  </appender>
  <root>
    <level value="WARNING" />
    <appender-ref ref="AdoLog4NetAppender" />
  </root>
  <logger additivity="false" name="Tracking">
    <level value="INFO"/>
    <appender-ref ref="AdoNetTrackingAppender" />
  </logger>
</log4net>

汇编名称的Common是否会找不到类型错误?

新错误如下:

    log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An established connection was aborted by the software in your host machine.) ---> System.ComponentModel.Win32Exception (0x80004005): An established connection was aborted by the software in your host machine
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
   at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
   at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
   at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName, Boolean shouldReconnect)
   at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName)
   at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.Common.DbConnection.System.Data.IDbConnection.BeginTransaction()
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)
ClientConnectionId:6f780eae-73cd-4c2d-bc26-545b3234f161
Error Number:10053,State:0,Class:20

我即将在内部公司服务器上安装更新的配置部分,而不是在本地进行测试。我想知道最新的错误是否是由于VPN每12小时回收一次。通过谷歌搜索错误,我还担心由于不活动而导致SQL服务器关闭连接。如果是这种情况,我会怀疑错误标签上的重新连接设置为true来解决这个问题。关于发生了什么的任何想法?

0 个答案:

没有答案