ASP.NET Core (.Net6)Log4Net日志记录

1.在nuget中引入下图的两个包

ASP.NET Core (.Net6)Log4Net日志记录_第1张图片

 2.在项目中新增log4net.config文件

ASP.NET Core (.Net6)Log4Net日志记录_第2张图片

3.配置文件内容4

 

	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
			
			
			
			
		
	

	
	
		
		
		
		
		
		
		
		
			
		
	

	
	
		
		
		
		
		
		
		
		
			
		
	
	
	
	
		
		
		
		
		
		
		
		
			
		
	

	
	
		
		
	

	
	
		
		
	

	
	
		
		
	
	
	
	
		
		
	

	
	
		
		
		
	
 

4.在startup中注入

ASP.NET Core (.Net6)Log4Net日志记录_第3张图片

或者在Program.cs中加入以下代码 

program.cs

var builder = WebApplication.CreateBuilder(args);
 
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSignalR();

builder.Services.AddLogging(cfg =>
{
    //cfg.AddLog4Net();
    //默认的配置文件路径是在根目录,且文件名为log4net.config
    //如果文件路径或名称有变化,需要重新设置其路径或名称
    //比如在项目根目录下创建一个名为cfg的文件夹,将log4net.config文件移入其中,并改名为log.config
    //则需要使用下面的代码来进行配置
    cfg.AddLog4Net(new Log4NetProviderOptions()
    {
        Log4NetConfigFileName = "ConfigFile/log4net.config",
        Watch = true
    });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();


app.Run();
 

 5.写一个log类

using System;

namespace CompoundingDevice.Common
{
    public class LogHelper
    {
        private static readonly log4net.ILog LInfo = log4net.LogManager.GetLogger("LogInfo");

        private static readonly log4net.ILog LError = log4net.LogManager.GetLogger("LogError");

        private static readonly log4net.ILog LMonitor = log4net.LogManager.GetLogger("LogMonitor");

        private static readonly log4net.ILog LDebug = log4net.LogManager.GetLogger("LogDebug");
        /// 
        /// 记录Error日志
        /// 
        /// 
        /// 
        public static void Error(string errorMsg, Exception ex = null)
        {
            if (ex != null)
            {
                LError.Error(errorMsg, ex);
            }
            else
            {
                LError.Error(errorMsg);
            }
        }

        /// 
        /// 记录Info日志
        /// 
        /// 
        /// 
        public static void Info(string msg, Exception ex = null)
        {
            if (ex != null)
            {
                LInfo.Info(msg, ex);
            }
            else
            {
                LInfo.Info(msg);
            }
        }

        /// 
        /// 记录Monitor日志
        /// 
        /// 
        public static void Monitor(string msg)
        {
            LMonitor.Info(msg);
        }

        /// 
        /// 记录Debug日志
        /// 
        /// 
        /// 
        public static void Debug(string msg, Exception ex = null)
        {
            if (ex != null)
            {
                LInfo.Debug(msg, ex);
            }
            else
            {
                LInfo.Debug(msg);
            }
        }

    }
}

6.使用方法

 LogHelper.Info($"发送数据 {cmd}");

你可能感兴趣的:(asp.net,core,Log4net,Log4net)