Log4Net - 如何添加仅用于特定代码部分的第二个记录器
我正在使用 Log4Net 2.0,它可以按要求工作,但可以为特定日志语句添加另一个记录器。所有日志记录都完成到单个文件附加程序。我想要做出的更改是,第 3 方应用程序会触发包含调试/错误信息的事件,我可以捕获此信息,虽然很有用,但我觉得它会污染正常的应用程序日志文件,并且更希望将其存储在自己的日志中文件。
我想要的最终结果是一个名为 log-file.txt
的文件,其中包含应用程序中除第 3 方日志记录之外的所有日志记录。第二个文件名为 log-file-3rdparty.txt
,仅包含来自第 3 方应用程序的日志记录。我遇到的问题是将 Log4Net 设置为有 2 个独立的记录器。我已经尝试创建第二个 LogFileAppender 并将其添加到根目录中,但是所有这一切都会将相同的日志记录放入两个记录器中。
在整个应用程序中,我们当前的 GetLogger 语句如下。理想情况下,这需要保持不变,因此现有的日志记录不会改变。我需要一个不受此语句影响的新记录器,而是纯粹为第三方日志记录而实例化。
private readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
以及 App.Config 如下。
< log4net>
<logger name="MyApp.Logging">
<level value="DEBUG"/>
</logger>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value=" [Application started] " />
<param name="Footer" value="[Application Finished] "/>
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</ log4net>
你能帮忙吗?
编辑
如果有什么不同的话。第 3 方日志事件在同一个类中捕获,我的一些应用程序日志事件也是从该类中触发的,这是因为该类负责控制第 3 部分软件。这是一个问题吗?我确实有一些想法,log4net 可以根据类名区分要创建的记录器,如果是这种情况,我是否需要将第 3 方日志记录重构为自己的类?
I'm using Log4Net 2.0, I have it working as required but which to add another logger for specific log statements. All logging is done to a single file appender. The change I want to make is that a 3rd party application fires events containing debug/error information, I can catch this information and whilst useful I feel it pollutes the normal application log file, and would instead prefer this to be stored in its own log file.
The end result I would like is a file called log-file.txt
with all the logging from the application except the 3rd party logging. And a 2nd file called log-file-3rdparty.txt
with logging only from the 3rd party application. The problem I have is setting up Log4Net to have 2 seperate loggers. I have already tried creating a 2nd LogFileAppender and adding it to the root however all this does in puts the same logging statements into both loggers.
Across the application we currently have the GetLogger statement as follows. This ideally needs to stay the same, so existing logging is unchanged. I need a new logger that is not affected by this statement, but is instead instantiated purely for 3rd party logging.
private readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
And the App.Config as follows.
< log4net>
<logger name="MyApp.Logging">
<level value="DEBUG"/>
</logger>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="
[Application started]
" />
<param name="Footer" value="[Application Finished]
"/>
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</ log4net>
Can you help?
EDIT
If it makes any difference. The 3rd party log events are captured in the same class that some of my application log events are also fired from, this is because the class is responsible for controlling the 3rd part software. Is this an issue? I do have some inkling that log4net can differentiate which logger to create based on class names, and if that's the case do i need to refactor the 3rd party logging to its own class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松创建一个特殊的记录器,如下所示:
然后您可以配置系统以为此记录器使用专用的附加程序:
You can easily create a special logger like this:
You can then configure the system to use a dedicated appender for this logger: