当前位置:网站首页>C # use log4net plug-in to output logs to files
C # use log4net plug-in to output logs to files
2022-07-26 06:42:00 【Hibiscus jasminoides】
One log file a day , It includes time , The log level , type , Information
Log output style :

stay Visual Studio in Nuget Search and download in package management log4Net plug-in unit

Use log4net You have to configure it yourself log4net Configuration file for , at present , The configuration file is XML Compiling . There are generally two ways , One is to use log4net Automatically generated “log4net.xml” To configure , The other is embedded directly into the running program app.config In file .
Creating log4net.xml After that, you need to set the copy output directory in the file attribute : Always copy
Log will be output to xml Under the folder specified in

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs/cmm.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%02thread] %-5level %logger: %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- Define file storage location -->
<file value="Log\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.txt'" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<!-- The text at the end of each log -->
<!-- Output format -->
<!-- Examples :2008-03-26 13:42:32,111 [10] INFO MainClass - info-->
<conversionPattern value="%newline%date [%thread] [%-5level] [%logger] :%message" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="Warn" />
<foreColor value="Yellow,HighIntensity" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="White" />
</mapping>
<mapping>
<level value="Debug" />
<foreColor value="Purple, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />
</layout>
</appender>
</log4net>Add another Log Log tool class , Customize the output content format
using log4net;
using System;
namespace Log4net
{
public static class Log
{
private static ILog log;
public static void Init(string name)
{
log = LogManager.GetLogger(name);
}
public static void Info(object message)
{
log.Info(message);
}
public static void InfoFormat(string format, object arg0)
{
log.InfoFormat(format, arg0);
}
public static void InfoFormat(string format, object arg0, object arg1)
{
log.InfoFormat(format, arg0, arg1);
}
public static void InfoFormat(string format, object arg0, object arg1, object arg2)
{
log.InfoFormat(format, arg0, arg1, arg2);
}
public static void InfoFormat(string format, params object[] args)
{
log.InfoFormat(format, args);
}
public static void Warning(object message)
{
log.Warn(message);
}
public static void WarningFormat(string format, object arg0)
{
log.WarnFormat(format, arg0);
}
public static void WarningFormat(string format, object arg0, object arg1)
{
log.WarnFormat(format, arg0, arg1);
}
public static void WarningFormat(string format, object arg0, object arg1, object arg2)
{
log.WarnFormat(format, arg0, arg1, arg2);
}
public static void WarningFormat(string format, params object[] args)
{
log.WarnFormat(format, args);
}
public static void Error(object message)
{
log.Error(message);
}
public static void ErrorFormat(string format, object arg0)
{
log.ErrorFormat(format, arg0);
}
public static void ErrorFormat(string format, object arg0, object arg1)
{
log.ErrorFormat(format, arg0, arg1);
}
public static void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
log.ErrorFormat(format, arg0, arg1, arg2);
}
public static void ErrorFormat(string format, params object[] args)
{
log.ErrorFormat(format, args);
}
public static void Fatal(object message)
{
log.Fatal(message);
}
public static void FatalFormat(string format, object arg0)
{
log.FatalFormat(format, arg0);
}
public static void FatalFormat(string format, object arg0, object arg1)
{
log.FatalFormat(format, arg0, arg1);
}
public static void FatalFormat(string format, object arg0, object arg1, object arg2)
{
log.FatalFormat(format, arg0, arg1, arg2);
}
public static void FatalFormat(string format, params object[] args)
{
log.FatalFormat(format, args);
}
}
}
Initialize when the code is running
using System.IO;
public class Program
{
static void Main(string[] args)
{
FileInfo fi = new FileInfo("log4net.xml");
log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);
Log.Init(" Initialize log plug-in !");
Log.Info(" Output log information !");
}
}Log plugin
边栏推荐
- 堆排序(heap-sort)
- 【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
- Design principle of infrared circuit of single chip microcomputer
- What are the aspects of performance testing? What are the classification and testing methods?
- Go channel
- Open includeexceptiondetailinfaults on the server (from servicebehaviorattribute or from & lt; servicedebug & gt; to configure behavior) to send exception information back
- [C language] file operation
- openssl: error while loading shared libraries: libssl.so.1.1
- Find the original root
- Advanced C language - archived address book (file)
猜你喜欢

Do you think you are a reliable test / development programmer? "Back to the pot"? Surface and reality

带你搞透IO多路复用原理(select、poll和epoll)

Show you the principle of IO multiplexing (select, poll and epoll)

【毕业季_进击的技术er】送别过去两年迷茫的自己。重整旗鼓,大三我来啦

vulnhub Lampião: 1

UIToolkit工具模板工程

Press in and pop-up sequence of "Niuke | daily question" stack

A tool for quickly switching local host files -- switchhosts

Proxyman, a native high-performance packet capturing tool, is for you who love learning
![[C language] file operation](/img/19/5bfcbc0dc63d68f10155e16d99581c.png)
[C language] file operation
随机推荐
09 eth smart contract
Overview of image classification of vision transformer must read series
数据库性能测试(mysql)
Torth file read vulnerability (cnvd-2020-27769)
nuxt 配置主题切换
Use scanner to get multiple data types from the keyboard
Advanced C language - archived address book (file)
How does the national standard gb28181 protocol easygbs platform realize device video recording and set streaming IP?
Rust语言- Slice(切片)类型(&[u8])
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
Multi target detection
C#使用log4net插件,输出日志到文件
"Niuke | daily question" inverse Polish expression
[untitled]
BigDecimal变为负数
Quick sort
服装行业如何实现数字化生产模式
Use and analysis of show profile optimized by MySQL
[1]数学建模基础入门知识
mysql优化之show profile的使用及分析