当前位置:网站首页>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);}}}
边栏推荐
- Static & dynamic & file address book
- [training Day6] game [mathematics]
- [training Day9] rotate [violence] [thinking]
- How to learn automated testing
- (posted) differences and connections between beanfactory and factorybean
- [Extension Program - cat scratch 1.0.15 _ online video and audio acquisition artifact _ installation tutorial plus acquisition]
- [summary of Feature Engineering] explain what features are and the steps of feature engineering
- Opencv learning Day2
- Selenium is detected as a crawler. How to shield and bypass it
- Summary of yarn Explorer
猜你喜欢
Hilditch refinement (implementation I)

Solution: 2003 cant connect to MySQL server on * * * * and use near 'identified by' * * * * 'with grant option' at
![[summary of Feature Engineering] explain what features are and the steps of feature engineering](/img/29/f9643affd3aaf9d83c5b81394d29a7.png)
[summary of Feature Engineering] explain what features are and the steps of feature engineering

C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms
![[Extension Program - cat scratch 1.0.15 _ online video and audio acquisition artifact _ installation tutorial plus acquisition]](/img/75/5eca7f63758802ecf86a90a1bbdeaf.png)
[Extension Program - cat scratch 1.0.15 _ online video and audio acquisition artifact _ installation tutorial plus acquisition]

Evolution of network IO model

what? Does the multi merchant system not adapt to app? This is coming!
![[training Day8] series [matrix multiplication]](/img/00/06f7eb935bfd3c195e2e135197b4bd.png)
[training Day8] series [matrix multiplication]

A simple method of converting SVG to PDF

ma.glasnost.orika. MappingException:No converter registered for conversion from Date to LocalDateTime
随机推荐
[training Day8] series [matrix multiplication]
Analysis of xmldecoder parsing process
Lecun proposed that mask strategy can also be applied to twin networks based on vit for self supervised learning!
What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews
Bring new people's experience
[advanced data mining technology] Introduction to advanced data mining technology
When using vscode, the tab indentation changes from 4 spaces to small arrows (solved)
RESNET interpretation and 1 × 1 Introduction to convolution
Evaluation of four operation expressions
Hilditch refinement (implementation I)
What does software testing need to learn?
Solution: 2003 cant connect to MySQL server on * * * * and use near 'identified by' * * * * 'with grant option' at
API data interface for historical data of A-share index
(posted) differences and connections between beanfactory and factorybean
Experience of using dump file to reverse locate crash location
API data interface of A-share transaction data
Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid
Fluoronisin peptide nucleic acid oligomer complex | regular active group alkyne, SH thiol alkynyl modified peptide nucleic acid
Modulenotfounderror: no module named 'pysat.solvers' (resolved)
Processing of null value of Oracle notes