quote dll : log4net.dll
Interface class :ILogger.cs
using System; using System.Collections.Generic; using System.Linq; using
System.Text; namespace HCCD.Base.Comm.NewLogger { /// <summary> /// Log interface ///
</summary> public interface ILogger { /// <summary> /// Debug log output /// </summary>
/// <param name="msg"> Output content </param> void Debug(string msg); /// <summary> ///
Debug log output /// </summary> /// <param name="msg"> Output content </param> /// <param
name="ex"> Abnormal output </param> void Debug(string msg, Exception ex); /// <summary> ///
Information log output /// </summary> /// <param name="msg"> Output content </param> void Info(string msg);
/// <summary> /// Information log output /// </summary> /// <param name="msg"> Output content </param> ///
<param name="ex"> Abnormal output </param> void Info(string msg, Exception ex); ///
<summary> /// Warning log output /// </summary> /// <param name="msg"> Output content </param> void
Warn(string msg); /// <summary> /// Warning log output /// </summary> /// <param
name="msg"> Output content </param> /// <param name="ex"> Abnormal output </param> void Warn(string msg,
Exception ex); /// <summary> /// Error log output /// </summary> /// <param
name="msg"> Output content </param> void Error(string msg); /// <summary> /// Error log output ///
</summary> /// <param name="msg"> Output content </param> /// <param name="ex"> Abnormal output </param>
void Error(string msg, Exception ex); /// <summary> /// Fatal log output /// </summary>
/// <param name="msg"> Output content </param> void Fatal(string msg); /// <summary> ///
Fatal log output /// </summary> /// <param name="msg"> Output content </param> /// <param
name="ex"> Abnormal output </param> void Fatal(string msg, Exception ex); } }
Interface implementation class :LogHelper.cs
using System; using System.Collections.Generic; using System.Linq; using
System.Text; using log4net; using log4net.Appender; using System.IO; using
log4net.Config; namespace HCCD.Base.Comm.NewLogger { /* * Author: Long * Date:
2019-09 * Detail: Log4net Log class */ /// <summary> /// Log4net Log class /// </summary>
public class LogHelper : ILogger { private Dictionary<string, ILog> LogDic =
new Dictionary<string, ILog>(); private object _islock = new object(); private
string fileName = string.Empty; /// <summary> /// Log call initialization /// </summary> ///
<param name="fileSavePath"> Log file save path [ If the path is empty , The default program root directory Logger folder ;]</param> ///
<param name="fileName"> Log file name [ If the file name is empty , The default file name :Default]</param> public
LogHelper(string fileSavePath, string fileName,string logSuffix = ".log") { try
{ Init(); if (string.IsNullOrEmpty(fileSavePath)) fileSavePath = "Logger"; if
(string.IsNullOrEmpty(fileName)) fileName = "Default"; this.fileName =
fileName; var repository = LogManager.GetRepository(); var appenders =
repository.GetAppenders(); if (appenders.Length == 0) return; var targetApder =
appenders.First(p => p.Name == "FileInfoAppender") as RollingFileAppender;
targetApder.File = Path.Combine(fileSavePath, this.fileName + logSuffix);
targetApder.ActivateOptions(); } catch (Exception ex) { } } /// <summary> ///
Cache log objects /// </summary> /// <param name="name"></param> /// <returns></returns>
private ILog GetLog(string name) { try { if (LogDic == null) { LogDic = new
Dictionary<string, ILog>(); } lock (_islock) { if (!LogDic.ContainsKey(name)) {
LogDic.Add(name, LogManager.GetLogger(name)); } } return LogDic[name]; } catch
{ return LogManager.GetLogger("Default"); } } /// <summary> /// Logging initialization ///
</summary> private void Init() { var file = new
FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"Log4net.config")); XmlConfigurator.Configure(file); } /// <summary> /// Debug log output
/// </summary> /// <param name="msg"> Output content </param> public void Debug(string msg)
{ var log = GetLog(this.fileName); if (log == null) { return; } log.Debug(msg);
} /// <summary> /// Debug log output /// </summary> /// <param name="msg"> Output content </param>
/// <param name="ex"> Abnormal output </param> public void Debug(string msg, Exception ex) {
var log = GetLog(this.fileName); if (log == null) { return; } log.Debug(msg,
ex); } /// <summary> /// Information log output /// </summary> /// <param
name="msg"> Output content </param> public void Info(string msg) { var log =
GetLog(this.fileName); if (log == null) { return; } log.Info(msg); } ///
<summary> /// Information log output /// </summary> /// <param name="msg"> Output content </param> ///
<param name="ex"> Abnormal output </param> public void Info(string msg, Exception ex) { var
log = GetLog(this.fileName); if (log == null) { return; } log.Info(msg, ex); }
/// <summary> /// Warning log output /// </summary> /// <param name="msg"> Output content </param>
public void Warn(string msg) { var log = GetLog(this.fileName); if (log ==
null) { return; } log.Warn(msg); } /// <summary> /// Warning log output /// </summary> ///
<param name="msg"> Output content </param> /// <param name="ex"> Abnormal output </param> public void
Warn(string msg, Exception ex) { var log = GetLog(this.fileName); if (log ==
null) { return; } log.Warn(msg, ex); } /// <summary> /// Error log output /// </summary>
/// <param name="msg"> Output content </param> public void Error(string msg) { var log =
GetLog(this.fileName); if (log == null) { return; } log.Error(msg); } ///
<summary> /// Error log output /// </summary> /// <param name="msg"> Output content </param> ///
<param name="ex"> Abnormal output </param> public void Error(string msg, Exception ex) { var
log = GetLog(this.fileName); if (log == null) { return; } log.Error(msg, ex); }
/// <summary> /// Fatal log output /// </summary> /// <param name="msg"> Output content </param>
public void Fatal(string msg) { var log = GetLog(this.fileName); if (log ==
null) { return; } log.Fatal(msg); } /// <summary> /// Fatal log output /// </summary> ///
<param name="msg"> Output content </param> /// <param name="ex"> Abnormal output </param> public void
Fatal(string msg, Exception ex) { var log = GetLog(this.fileName); if (log ==
null) { return; } log.Fatal(msg, ex); } } }
Configuration class :Log4net.config
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections> <!-- Site log configuration section --> <log4net> <!-- OFF, FATAL, ERROR, WARN, INFO,
DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root>
<!-- Control level , From low to high : ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<!-- For example, the definition level is INFO, be INFO Level down , such as DEBUG The log will not be recorded --> <!-- If there is no definition LEVEL Value of , The default is DEBUG-->
<level value="ALL"/> <appender-ref ref="FileInfoAppender"/> </root> <appender
name="FileInfoAppender" type="log4net.Appender.RollingFileAppender">
<!-- Minimum locking for multithreading --> <lockingModel
type="log4net.Appender.FileAppender+MinimalLock"/> <file/> <param
name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/>
<datePattern value=""."yyyy-MM-dd".log""/> <!-- Fixed file name -->
<param name="StaticLogFileName" value="true"/> <encoding value="utf-8"/>
<!-- Log output format --> <layout type="log4net.Layout.PatternLayout,log4net"> <param
name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%p] %m%n"/> </layout>
</appender> </log4net> </configuration>
Call mode
quote :log4net.dll
HCC.Base.Comm.NewLogger.dll
realization :
using System; using System.Collections.Generic; using System.Linq; using
System.Text; using HCC.Base.Comm.NewLogger; namespace ConsoleApplication1 {
class Program { static void Main(string[] args) { LogHelper log = new
LogHelper(@"D:\Logger", "stydy"); log.Info(" The down payment of Geda Sange in Guangdong Province is good for single nuclear radiation ");
log.Info(" The down payment of Geda Sange in Guangdong Province is good for single nuclear radiation ", new Exception(" Please check the system exception ! This problem is quite serious ."));
log.Fatal("gdsgdsgdsgdsgdsgds"); log.Fatal("gdsgdsgdsgdsgdsgds", new
Exception(" Please check the system exception ! This is a serious problem .")); log.Warn(" Guangdong Geda Sange TV cabinet ");
log.Warn("gdsgdsgdsgdsgdsgds", new Exception(" Please check the system exception ! This problem is quite serious ."));
log.Debug("cbvcbvfgsfgdsgdsgdsgdsgdsgds"); log.Debug("gdsgdsgdsgdsgdsgds", new
Exception(" Please check the system exception ! This problem is quite serious .")); log.Error(" System abnormality "); log.Error(" System abnormality ",new
Exception(" Please check the system ! This problem is quite serious .")); } } }
Technology