webapi使用log4net在release模式下无法生成文件的解决方案

为 WebAPI  添加 log4net 日志功能,

LogNet 的配置信息,  网络几乎都是和 Web.Config  写在一起啊的 。

今天发布一个webapi,在Debug模式一切正常,但是在release模式下log4net不工作,查了很多资料,终于解决。具体做如下检查修改。

1.从nuget下载log4net

2.一个log4net.config文件,各个节点的意义详细查询api,这是产生文件的路径“logs/info/2020/202003/20200301.log”,logs目录下日志分会按debug,error,fatal,info,warn来存在日志。

log4net.config配置内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <!--配置一个结点 名称为log4net-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <root>
      <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
      <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
      <!--如果没有定义LEVEL的值,则缺省为DEBUG-->
      <level value="ALL" />
      <appender-ref ref="INFOAppender" />
      <appender-ref ref="DEBUGAppender" />
      <appender-ref ref="WARNAppender" />
      <appender-ref ref="ERRORAppender" />
      <appender-ref ref="FATALAppender" />
      <appender-ref ref="ColoredConsole" />
      <appender-ref ref="TraceAppender" />
      <appender-ref ref="ADONetAppender_SqlServer"/>
    </root>

    <!--写入到文件-->
    <appender name="INFOAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\info\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <!--"%d %t %p %l %m %n”:  
         1、%d输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss},输出类似:2005-7-19 17:49:27,刚好适合插入SQLServer;  
         2、%t  产生该日志事件的线程名;  
         3、%p 日志的log_level,如DEBUG、WARN或者INFO;  
         4、%c  输出所属的类目,通常就是所在类的全名,如“iNotes.Default”;  
         5、%m 日志的内容;  
         6、%l  输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。如write2database.main(write2database.java:33);  
         7、%n  输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”  
        -->
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO"/>
        <param name="LevelMax" value="INFO"/>
      </filter>
    </appender>
    <appender name="DEBUGAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\debug\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="DEBUG"/>
      </filter>
    </appender>
    <appender name="WARNAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\warn\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="WARN"/>
        <param name="LevelMax" value="WARN"/>
      </filter>
    </appender>
    <appender name="ERRORAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\error\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR"/>
        <param name="LevelMax" value="ERROR"/>
      </filter>
    </appender>
    <appender name="FATALAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\fatal\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="FATAL"/>
        <param name="LevelMax" value="FATAL"/>
      </filter>
    </appender>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="conversionPattern" value="%newline*******************************************%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline*******************************************%newline" />
      </layout>
    </appender>

    <!--将日记写入数据库-->
    <appender name="ADONetAppender_SqlServer" type="log4net.Appender.ADONetAppender">
      <bufferSize value="0" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Server=192.168.4.253;Database=SOADB;User Id=sa;Password=qwer1234;" />
      <commandText value="INSERT INTO UDT_SYS_Log([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <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="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
  </log4net>
</configuration>

  

一般都是在AssemblyInfo.cs文件中写入如下代码

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

如图:

 

 

但这个方式在Debug模式下没有问题,但是在release模式下就不好用了,需要在Global.asax文件中具体再次指定配置文件所在位置,例如:

log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~") + @"\log4net.config"));

 如图:

 

 

重新编译发布就可以了。

 

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <!--配置一个结点 名称为log4net-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <root>
      <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
      <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
      <!--如果没有定义LEVEL的值,则缺省为DEBUG-->
      <level value="ALL" />
      <appender-ref ref="INFOAppender" />
      <appender-ref ref="DEBUGAppender" />
      <appender-ref ref="WARNAppender" />
      <appender-ref ref="ERRORAppender" />
      <appender-ref ref="FATALAppender" />
      <appender-ref ref="ColoredConsole" />
      <appender-ref ref="TraceAppender" />
      <appender-ref ref="ADONetAppender_SqlServer"/>
    </root>

    <!--写入到文件-->
    <appender name="INFOAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\info\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <!--"%d %t %p %l %m %n”:  
         1、%d输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss},输出类似:2005-7-19 17:49:27,刚好适合插入SQLServer;  
         2、%t  产生该日志事件的线程名;  
         3、%p 日志的log_level,如DEBUG、WARN或者INFO;  
         4、%c  输出所属的类目,通常就是所在类的全名,如“iNotes.Default”;  
         5、%m 日志的内容;  
         6、%l  输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。如write2database.main(write2database.java:33);  
         7、%n  输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”  
        -->
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO"/>
        <param name="LevelMax" value="INFO"/>
      </filter>
    </appender>
    <appender name="DEBUGAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\debug\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="DEBUG"/>
      </filter>
    </appender>
    <appender name="WARNAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\warn\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="WARN"/>
        <param name="LevelMax" value="WARN"/>
      </filter>
    </appender>
    <appender name="ERRORAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\error\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR"/>
        <param name="LevelMax" value="ERROR"/>
      </filter>
    </appender>
    <appender name="FATALAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\\fatal\\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.log'" />
      <staticLogFileName value="false"/>
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="FATAL"/>
        <param name="LevelMax" value="FATAL"/>
      </filter>
    </appender>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="conversionPattern" value="%newline*******************************************%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline*******************************************%newline" />
      </layout>
    </appender>

    <!--将日记写入数据库-->
    <appender name="ADONetAppender_SqlServer" type="log4net.Appender.ADONetAppender">
      <bufferSize value="0" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Server=192.168.40.253;Database=SOADB;User Id=sa;Password=qwer1234;" />
      <commandText value="INSERT INTO UDT_SYS_Log([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <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="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>



  </log4net>
</configuration>

  

 

posted on 2020-03-01 14:07  狼来了  阅读(545)  评论(0编辑  收藏  举报