我有一个界面
public interface ILoggerService
{
void Info(string message);
void Warn(string message);
}
然后,我有一个使用 Log4Net 实现这个接口和日志的类
public class Log4NetLoggerService : ILoggerService
{
private readonly ILog _logger;
public Log4NetLoggerService()
{
// this always returns Log4NetLoggerService class type
_logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public void Info(string message)
{
_logger.Info(message);
}
}
这很好用,但问题是如果我想记录当前的类和方法名(使用 %class - %M),它总是Log4NetLoggerService
作为类返回,作为方法返回Info
作为方法。
我需要获取称为日志记录方法的“父”类类型。
我可以在创建ILoggerService
实例时以某种方式注入调用日志方法的类的类型吗?