当前位置:网站首页>A very useful log4net logging library
A very useful log4net logging library
2022-07-24 20:57:00 【biyusr】
stay .net In the project , Regular use log4net Class library to record the project log , If you need to record which class and which method , It is often necessary to define a static attribute in each class , Used to get which class and which method . This article introduces the use of reflection technology , Get the caller's class and method name .
First , At the program entrance , Initialize logging , Otherwise, the log file cannot be generated automatically .
Logger.Initialize();Later, you can reference this library anywhere , Use logging .
/// <summary>/// Start screen recording/// </summary>public string StartCaptureScreen(){if (RecordScreenParameter == null){Logger.Debug($" Screen recording parameters {nameof(RecordScreenParameter)} It's empty , Unable to perform screen recording !");}if (!RecordScreenParameter.IsEnabled){Logger.Debug($" You have not enabled the screen recording parameter {nameof(RecordScreenParameter.IsEnabled)}, Unable to perform screen recording !");}}
Error log
try{}catch (Exception ex){Logger.Error(ex.Message);};
So how to get the method name of the caller through reflection technology , Here is the core code
public static void Debug(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Debug(message);}
The complete code is as follows :
using log4net;using System.Reflection;namespace System{/// <summary>/// Logging class/// </summary>public class Logger{/// <summary>/// initialization ILog/// </summary>public static void Initialize(){LogManager.GetLogger(typeof(Logger));}public static void Debug(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Debug(message);}public static void Info(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Info(message);}public static void Warn(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Warn(message);}public static void Error(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Error(message);}public static void Fatal(object message){MethodBase method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();var logger = LogManager.GetLogger(method.DeclaringType);#if DEBUGConsole.WriteLine(message);#endiflogger?.Fatal(message);}}}
边栏推荐
- [training Day10] silly [simulation] [greed]
- Go language structure
- ECCV 2022 open source | target segmentation for 10000 frames of video
- Do you want to enroll in a training class or study by yourself?
- Baidu PaddlePaddle easydl helps improve the inspection efficiency of high-altitude photovoltaic power stations by 98%
- Generate self signed certificate: generate certificate and secret key
- (posted) differences and connections between beanfactory and factorybean
- [training Day10] point [enumeration] [bidirectional linked list]
- [training Day8] interesting number [digital DP]
- Defects of matrix initialization
猜你喜欢

ma.glasnost.orika. MappingException:No converter registered for conversion from Date to LocalDateTime
![[advanced data processing technology] data filtering, advanced data filling, initial and advanced data transformation](/img/0c/70643343d73137926963013a4862d0.png)
[advanced data processing technology] data filtering, advanced data filling, initial and advanced data transformation
![[training Day6] dream [priority queue] [greed]](/img/1b/309b53618b8a116862971799ce4e78.jpg)
[training Day6] dream [priority queue] [greed]

Lua environment configuration
![[training Day6] triangle [mathematics] [violence]](/img/57/d603886c202de7d46ea03487b633f5.png)
[training Day6] triangle [mathematics] [violence]

Evolution of network IO model
![[training Day8] series [matrix multiplication]](/img/00/06f7eb935bfd3c195e2e135197b4bd.png)
[training Day8] series [matrix multiplication]

In the era of new knowledge economy, who is producing knowledge?

Pychart tutorial: 5 very useful tips

Actual measurement of Qunhui 71000 Gigabit Network
随机推荐
A simple method of converting SVG to PDF
Baidu classic interview question - determine prime (how to optimize?)
PC port occupation release
Detailed explanation of ThreadLocal
How to learn automated testing
Shenzhen Merchants Securities account opening? Is it safe to open a mobile account?
Oracle creates table spaces and views table spaces and usage
Mysql8 doesn't seem to support MyISAM partition tables. Does polardb-x support MyISAM partition tables?
The difference between token and session, this classic interview question deserves a more in-depth answer
English grammar_ Demonstrative pronoun this / these / that / those
Strong reference, weak reference, soft reference, virtual reference
Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry
Synthesis of peptide nucleic acid PNA labeled with heptachydrin dye cy7 cy7-pna
Put apples
Chrome realizes automated testing: recording and playback web page actions
Pychart tutorial: 5 very useful tips
API data interface of A-share transaction data
[summary of Feature Engineering] explain what features are and the steps of feature engineering
Selenium is detected as a crawler. How to shield and bypass it
(forward) usage of PostMessage