log4net C#中的方法名称 [英] Method Name in log4net C#
问题描述
我为log4net创建了一个c#包装器.
I created a c# wrapper for log4net.
它具有Debug()和Error()方法.
It has Debug() and Error() methods.
我想记录记录记录的方法名称,但是如果我尝试使用%method转换模式,它只会打印Debug(包装方法名称).
I want to log the method name which logs the record, but If I try to use the %method conversion pattern, it just prints Debug, which is the wrapper method name.
有没有办法打印完整的方法堆栈?
Is there a way to print the full method stack?
例如
代替Debug-> SomeLoggingActionInSomeClass.Debug?
Instead of Debug --> SomeLoggingActionInSomeClass.Debug?
包装器类代码:
public static class Logger
{
private static ILog _log;
static Logger()
{
log4net.Config.XmlConfigurator.Configure();
_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public static void Init()
{
}
public static void Debug(string message)
{
_log.Debug(message);
}
呼叫班级代码:
W6CustomizationLogger.Logger.Debug("Backup(): START");
推荐答案
废弃该包装器.
配置
要初始化日志记录,您可以在启动项目中轻松地对其进行配置.
To initialize logging you can instead configure it quite easily in your startup project.
- 在解决方案资源管理器中的启动项目上,单击属性"旁边的箭头
- 双击装配信息
3添加以下内容:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
3 Add the following:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
屏幕截图:
用法
现在在您的课程中只需添加:
Now in your classes simply add:
public class YourClass
{
private ILog _logger = LogManager.GetLogger(typeof(YourClass));
// [....]
}
在log4net.config
中,您现在可以在输出中使用logger
属性:
And in your log4net.config
you can now use the logger
property in the output:
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-7level %-40logger %message%newline" />
</layout>
这将在每条日志行上打印名称空间和类型名称(-7
和-40
填充名称,以便获得直列).
Which will print the namespace and type name on every log line (-7
and -40
pads the names so that I get straight columns).
另一个很棒的事情是,您还可以在名称空间上使用过滤器(使所有数据库类都记录到"databases.log"等).
The other great thing is that you can also use a filter on the namespace (to make all database classes log to "databases.log" etc).
<appender name="DatabaseAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\MyApp\Database.log" />
<rollingStyle value="Composite" />
<datePattern value=".yyyy-MM-dd'.log'" />
<appendToFile value="true" />
<maximumFileSize value="50MB" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level@%thread [%logger] %message%newline" />
</layout>
<!-- Namespace/Type filter -->
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="CompanyName.DatabaseNamespace"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
如果%logger
仅获取类名称,也可以使用%type{1}
代替.
You can also use %type{1}
instead if %logger
to get only the class name.
这篇关于log4net C#中的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!