本篇接扫 log4net 基本使用
一、导包
二、配置文件
Log4Net.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net-net-1.0"/> </configSections> <log4net> <appender name="AppLog" type="log4net.Appender.RollingFileAppender"> <param name="File" value="SysLog/" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="-1" /> <!--最小锁定模型以允许多个进程可以写入同一个文件--> <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="MaximumFileSize" value="10MB" /> <param name="RollingStyle" value="Size" /> <param name="DatePattern" value="yyyy-MM-dd" /> <param name="StaticLogFileName" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p %d [%c] %m%n" /> </layout> </appender> <logger name="AppLog"> <level value="all" /> <appender-ref ref="AppLog" /> </logger> <root> <level value="all" /> </root> </log4net> </configuration>
AppLog.cs
using log4net; using log4net.Appender; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Log4net { public class AppLog { private static string filepath = AppDomain.CurrentDomain.BaseDirectory + @"\SysLog\"; private static readonly log4net.ILog logComm = log4net.LogManager.GetLogger("AppLog"); static AppLog() { log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config")); if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } } public static readonly object o = new object(); /// <summary> /// 写入日志 /// </summary> /// <param name="msg">日志内容</param> /// <param name="isWrite">是否写</param> /// <param name="action">写日志的方法</param> /// <param name="info">日志文件名,便于分开日志文件</param> private static void WriteLog(string msg, bool isWrite, Action<object> action, string info = "") { if (isWrite) { lock (o) { string filename = $"AppLog_{action.Method.Name}_{info}_{ DateTime.Now.ToString("yyyyMMdd_HH")}.log"; var repository = LogManager.GetRepository(); #region MyRegion var appenders = repository.GetAppenders(); if (appenders.Length > 0) { RollingFileAppender targetApder = null; foreach (var Apder in appenders) { if (Apder.Name == "AppLog") { targetApder = Apder as RollingFileAppender; break; } } if (targetApder.Name == "AppLog")//如果是文件输出类型日志,则更改输出路径 { if (targetApder != null) { if (!targetApder.File.Contains(filename)) { targetApder.File = @"SysLog\" + filename; targetApder.ActivateOptions(); } } } } #endregion action(msg); //logComm.Error(msg + "\n"); } } } /// <summary> /// /// </summary> /// <param name="msg">日志内容</param> /// <param name="info">日志文件名,便于分开日志文件</param> /// <param name="isWrite">是否写入</param> public static void WriteError(string msg, string info = "", bool isWrite = true) { WriteLog(msg, isWrite, logComm.Error, info); } public static void WriteInfo(string msg, string info = "", bool isWrite = true) { WriteLog(msg, isWrite, logComm.Info, info); } public static void WriteWarn(string msg, string info = "", bool isWrite = true) { WriteLog(msg, isWrite, logComm.Warn, info); } public static void WriteFatal(string msg, string info = "", bool isWrite = true) { WriteLog(msg, isWrite, logComm.Fatal, info); } } }
三、使用
AppLog.WriteError("用户[张三]:点击了执行了步骤1,发送错误");
标签:info,string,C#,static,msg,日志,isWrite From: https://www.cnblogs.com/zeussbook/p/18332959