Log4Net 常见错误提示(不断更新中)

1. 无法识别log4中的节点,如:<section>等

解决办法:在configrition中直接申明log4

<configSections><!--必须为第一个节点-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

2.无法创建txt日志

解决办法:请查看应用的log4配置文件路径是否正确

3.按照时间和大小无法分割文件

解决办法:1)必须使用param name= 才可以; 2)是否将文件名设置为固定不变

4.将日志文件放在用户目录下的我的文档(特殊路径)

解决办法:

             1)英文版系统版本 

<file value="%USERPROFILE%My DocumentsMyApplog.txt"/>

  

             2)非英文系统版本  

<file type="log4net.Util.PatternString" value="%envFolderPath{MyDocuments}MyApplog.txt" />

5.定义自己的消息版本

             1) Modify the command text: 

INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn)

             2) Add the parameter definition for the custom column:

<parameter>
   <parameterName value="@CustomColumn"/>
   <dbType value="String" />
   <size value="255" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{CustomColumn}" />
  </layout>
</parameter>

           3) Then use one of log4net’s contexts to transfer values to the parameter:

// thread properties...
log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";
log.Info("Message"); 

// ...or global properties
log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";
原文地址:https://www.cnblogs.com/alinawang/p/9541356.html