之前介绍了log4net的在.net core下的使用方法,但是每次调用起来都比较麻烦
private ITestService _TestService;
private IUserInfo _UserInfo;
private readonly ILogger<testController> _logger;
public testController(ITestService TestService,
IUserInfo UserInfo,
ILogger<testController> logger) {
this._TestService = TestService;
_UserInfo = UserInfo;
_logger = logger;
}
那其实我们可以自定义Log4NetHelper来简单点使用
using log4net;
using MyUtility.Config;
using System;
using System.Collections.Generic;
using System.Text;
namespace MyUtility.Log {
public class Log4NetHelper {
public static void WriteInfoLog(Type type, object info) {
var repositoryName = ConfigHelper.GetValue("Log4NetRepository");
ILog log = LogManager.GetLogger(repositoryName,type);
log.Info(info);
}
public static void WriteErrorLog(Type type,Exception exception) {
var repositoryName = ConfigHelper.GetValue("Log4NetRepository");
ILog log = LogManager.GetLogger(repositoryName,type);
log.Error("Error", exception);
}
}
}
其他配置跟之前的一样,在需要调用记录日志的地方只要
Log4NetHelper.WriteInfoLog(typeof(testController), "testlog");
就行了,只要相对来说就简单点了,少了几行代码
来源:CSDN
作者:Sammy爱学习
链接:https://blog.csdn.net/sammy520/article/details/104173185