首页 > 其他分享 >如何把 log4net.config 文件删除, 改成代码实现

如何把 log4net.config 文件删除, 改成代码实现

时间:2024-05-15 17:42:34浏览次数:27  
标签:log4net Info infoAppender RollingFileAppender new var 改成 errorAppender config

如何把 log4net.config 文件改成代码?

这样在编写winform或者其他客户端的时候就省去了一个配置文件

分享下我常用的log4net.config 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!--<configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
    </configSections>-->
  <log4net>
    <!--console-->
    <!--<appender name="RollingFileAppender_Debug" type="log4net.Appender.ManagedColoredConsoleAppender">
      <encoding value="utf-8"/>
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="Gray" />
      </mapping>
      <mapping>
        <level value="Info" />
        <foreColor value="Yellow" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>-->

    <!-- RollingFileAppender to log the messages that their level are DEBUG and INFO -->
    
    <appender name="RollingFileAppender_Debug" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/Debug/" />
      <encoding value="utf-8"/>
      <appendToFile value="true" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100MB" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy-MM-dd.'log'" />
      <staticLogFileName value="false" />
      <!-- Set filter,LevelRangeFilter -->
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n------------------------------------------------------------------------------------------------%n%n" />
      </layout>
    </appender>


    <!-- RollingFileAppender to log the messages that their level are DEBUG and INFO -->
    <appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/Info/" />
      <encoding value="utf-8"/>
      <appendToFile value="true" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100MB" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy-MM-dd.'log'" />
      <staticLogFileName value="false" />
      <!-- Set filter,LevelRangeFilter -->
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[%d]%m%n" />
      </layout>
    </appender>

    <!-- RollingFileAppender to log the messages that their level are WARN or higher -->
    <appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/Error/" />
      <encoding value="utf-8"/>
      <appendToFile value="true" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="20MB" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyy-MM-dd.'log'" />
      <staticLogFileName value="false" />
      <!-- Set filter,LevelRangeFilter -->
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n[异常:%exception]%n------------------------------------------------------------------------------------------------%n%n" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <!--<appender-ref ref="RollingFileAppender_Debug" />-->
      <appender-ref ref="RollingFileAppender_Info" />
      <appender-ref ref="RollingFileAppender_Error" />
    </root>
  </log4net>
</configuration>

只需要在程序启动的时候执行下下面的代码,其他代码不变就可以直接使用了.

var hierarchy = (Hierarchy)LogManager.GetRepository();
var infoAppender = new RollingFileAppender
{
    Name = "RollingFileAppender_Info",
    File = $"Logs/Info/",
    Encoding = System.Text.Encoding.UTF8,
    AppendToFile = true,
    MaxSizeRollBackups = 10,
    MaximumFileSize = "10MB",
    RollingStyle = RollingFileAppender.RollingMode.Composite,
    DatePattern = "yyyy-MM-dd'.log'",
    StaticLogFileName = false
};

var infoFilter = new LevelRangeFilter
{
    LevelMin = Level.Info,
    LevelMax = Level.Info
};
infoAppender.AddFilter(infoFilter);
var infoLayout = new PatternLayout
{
    ConversionPattern = "[%date]%message%newline"
};
infoLayout.ActivateOptions();

infoAppender.Layout = infoLayout;
infoAppender.ActivateOptions();

hierarchy.Root.AddAppender(infoAppender);

var errorAppender = new RollingFileAppender
{
    Name = "RollingFileAppender_Error",
    File = $"Logs/Error/",
    Encoding = System.Text.Encoding.UTF8,
    AppendToFile = true,
    MaxSizeRollBackups = 10,
    MaximumFileSize = "20MB",
    RollingStyle = RollingFileAppender.RollingMode.Composite,
    DatePattern = "yyyy-MM-dd'.log'",
    StaticLogFileName = false
};

var errorFilter = new LevelRangeFilter
{
    LevelMin = Level.Error,
    LevelMax = Level.Fatal
};
errorAppender.AddFilter(errorFilter);
var errorLayout = new PatternLayout
{
    ConversionPattern = "[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n[异常:%exception]%n------------------------------------------------------------------------------------------------%n%n"
};
errorLayout.ActivateOptions();

errorAppender.Layout = errorLayout;
errorAppender.ActivateOptions();
hierarchy.Root.AddAppender(errorAppender);

BasicConfigurator.Configure();
private static readonly ILog _logger  = LogManager.GetLogger("YOUR CLASS NAME");

_logger.Info("你好");

标签:log4net,Info,infoAppender,RollingFileAppender,new,var,改成,errorAppender,config
From: https://www.cnblogs.com/HandLoong/p/18194380

相关文章

  • k8s——configmap-secret-nginx实验
    简介configmapsecret一、实验环境二、实验描述三、实验1:步骤1.使用configmap投射到nginx.conf配置文件到pod里1.1需要准备nginx.conf配置文件1.2将nginx.conf内容存放到configmap里(通过文件的方式,,这样简单一点)1.3启动ngnix的pod,使用configmap里的nginx.conf配置文件2.......
  • 创建启动springboot项目的一些问题,如spring-boot-autoconfigure 自动加载注入配置
    1.springboot项目启动是否只需要3下面3个jar包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.b......
  • pkg-config(转)
    pkg-config是一个linux下的命令,用于获得某一个库/模块的所有编译相关的信息。例子:pkg-configopencv–libs–cflags结果:-I/usr/include/opencv/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so/usr/lib/x86_64-linux-gnu/libopencv_contrib.so/usr/lib/x86_64-linux......
  • k3s集群默认config不在~/.kube/
    在K3s安装的集群上,kubeconfig文件通常位于服务器的/etc/rancher/k3s/k3s.yaml路径下。这个文件包含了连接到Kubernetes集群所需的配置信息,包括集群的服务器地址、CA证书、用户凭证等。找到kubeconfig文件:登录到K3s服务器,检查/etc/rancher/k3s/k3s.yaml文件是否存在。您可以使......
  • vite.config.ts配置
    来源:https://blog.csdn.net/qq_39275868/article/details/1262676321.配置和pinia,router,axios,ref,reactive引入等等2.配置代理3.配置.ts,.vue,.tsx等等文件别名4.配置antdV按需加载5.配置antdV主题色+全局引入less+全局颜色变量6.配置vue使用tsx写法7.配置测试环境保留打印import......
  • win11 系统右键菜单 更改成 win10的
    系统右键菜单切换的两份文件代码:to_win10.bat:win11换成win10的;to_win11.bat:win10换成win11的;to_win10.batregadd"HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"/f/veto_win11.batregdelete"HKCU\Software\Cl......
  • clickhuose config.xml文件
    <?xmlversion="1.0"?><yandex><logger><level>trace</level><log>/var/log/clickhouse-server/clickhouse-server.log</log><!--日志存放目录--><errorlog>/var/log/clic......
  • configure,make和make install关系
    一.参考网址1. configure、make、makeinstall背后的原理(翻译)2. configure,make和makeinstall关系3. Linux:configuremake、makeinstall ......
  • dbt on_configuration_change 简单说明
    dbton_configuration_change目前主要是在处理物化视图中,on_configuration_change包含了三类设置参考设置apply是默认参数,尝试进行更新存在的数据库对象continue允许继续运行,但是提供一个警告fail如果捕捉到变动就失败参考使用比如模型上的{{config(......
  • 代理 mitmproxy config.yaml 模板 使用笔记(二)
    代理mitmproxyconfig.yaml模板使用笔记(二)mitmproxyconfig.yaml模板使用mitmproxy可能需要用到config.yaml来批量配置参数目录config.yaml文件所在位置config.yaml配置模板文件位置配置文件默认读取路径:~/.mitmproxy/config.yaml,见配置项:confdir:'~/.mitmpro......