简体   繁体   English

Log4NET,ADONetAppender和自定义字段

[英]Log4NET, ADONetAppender and custom fields

I am able to set up log4NET with the ADONetAppender, and all works fine when I want to capture the state of things when I log a message via log.Info(message). 我能够使用ADONetAppender设置log4NET,并且当我想通过log.Info(message)记录消息时捕获事物的状态时,一切工作正常。

As I will be logging from various locations in my application based on an ActionID which changes throughout the application, how do I extend things so that I may instead issue a call such as log.Info(ActionID, message), with ActionID ending up in the database? 由于我将基于一个在整个应用程序中发生变化的ActionID从应用程序中的各个位置记录日志,因此如何扩展内容,以便我可以发出诸如log.Info(ActionID,message)之类的调用,而ActionID最终以数据库?

You can add custom fields to log4net using GlobalContext.Properties before calling log.Info() by doing something like this: 您可以通过执行以下操作,在调用log.Info()之前,使用GlobalContext.Properties将自定义字段添加到log4net:

GlobalContext.Properties["ActionID"] = ActionID;

Then, in your ADONetAppender configuration, you can access this custom field with %property{ActionID} . 然后,在ADONetAppender配置中,您可以使用%property{ActionID}访问此自定义字段。

I think that @bcwood probably has the best/easiest idea to simply use GlobalContext.Properties to store the id that you want to log. 我认为@bcwood大概是最好/最简单的想法,即简单地使用GlobalContext.Properties来存储要记录的ID。

However, if the id is very important to you and if you want to streamline the setting of the id (such as by adding a parameter to the various logging methods vs having to add a separate call to set the id value in the GlobalContext), then you might have a look at this folder in the log4net repository. 但是,如果ID对您来说非常重要,并且您想简化ID的设置(例如,通过向各种日志记录方法中添加参数,而不必在GlobalContext中添加单独的调用来设置ID值),那么您可以在log4net存储库中查看此文件夹。

http://svn.apache.org/viewvc/logging/log4net/trunk/extensions/net/1.0/log4net.Ext.EventID/cs/src/ http://svn.apache.org/viewvc/logging/log4net/trunk/extensions/net/1.0/log4net.Ext.EventID/cs/src/

It contains an example of how to extend the log4net logger to add an "EventID" parameter. 它包含有关如何扩展log4net记录器以添加“ EventID”参数的示例。

It seems pretty involved to me (subclass the log4net logger, create your own LogManager to dispense your loggers, etc). 对我来说似乎很复杂(将log4net记录器子类化,创建自己的LogManager来分配记录器,等等)。

You can probably go a little bit simpler if you choose to wrap log4net, and use log4net's logger internally to log. 如果选择包装log4net,并在内部使用log4net的记录器进行记录,则可能会更简单一些。

The advantage of using the approach that I linked to above (or a similar, but simpler approach of wrapping log4net's Logger), is that your logging call sites can look like you proposed: 使用我上面链接的方法(或包装log4net的Logger的类似但更简单的方法)的优点在于,您的日志记录调用站点看起来像您建议的那样:

logger.Info(123, "Hello");
logger.Info(321, "Good bye");

As compared to @bcwood's suggestion, which would make your logging call sites look like this: 与@bcwood的建议相比,这会使您的日志记录呼叫站点看起来像这样:

GlobalContext.Properties["ActionID"] = 123;
logger.Info("Hello");
GlobalContext.Properties["ActionID"] = 321;
logger.Info("Good bye");

Try creating a custom function by wrapping the info method. 尝试通过包装info方法来创建自定义函数。

void InfoLog(int ActionID, string message)
{
   log.info(String.Format("{0}:{1}",ActionID.ToString(),message));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM