Nlog:将异常详细信息记录到数据库时遇到问题。

时间:2015-03-23 00:55:08

标签: c# database exception nlog

我的nlog配置文件是

   <?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">

  <!-- 
  See https://github.com/nlog/nlog/wiki/Configuration-file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->

    <!--
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />           

    -->
    <target name="database" xsi:type="Database" keepConnection="true" useTransactions="true"
          dbProvider="System.Data.SqlClient"
            connectionString="Server=******\SQLEXPRESS;User Id=sa;Password=****;Database=*****_dev;MultipleActiveResultSets=true;"           
           commandText="INSERT INTO Logs(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (@EventDateTime, @EventLevel, 'SE', @MachineName, @EventMessage, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage)">
      <!-- parameters for the command -->
      <parameter name="@EventDateTime" layout="${date:s}" />
      <parameter name="@EventLevel" layout="${level}" />     
      <parameter name="@MachineName" layout="${machinename}" />      
      <parameter name="@ErrorSource" layout="${exception:format=ToString,StackTrace}" />
      <parameter name="@EventMessage" layout="${message}" />
      <parameter name="@ErrorClass" layout="${event-context:item=error-method}" />
      <parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
      <parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
      <parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
    </target>
  </targets>

  <rules>
    <!-- add your logging rules here -->


    <logger name="*" minlevel="Trace" writeTo="database" />

  </rules>
</nlog>

它只插入EventDateTime,EventLevel,UserName,MachineName和EventMessage但它将异常和事件上下文字段留空?

我显然遗漏了一些异常的东西,我无法弄清楚它是什么。

请参阅下面的C#代码。

 public void Log(Exception ex)
     {
             LogEventInfo ev = new LogEventInfo(LogLevel.Error, logger.Name ,ex.Message);
             ev.Exception = ex;               
             logger.Error(ev);             
     }

请提供建议

0 个答案:

没有答案