使用log4net或NLog的WCF日志记录/跟踪和活动ID传播 [英] WCF logging/tracing and activity id propagation using log4net or NLog

查看:133
本文介绍了使用log4net或NLog的WCF日志记录/跟踪和活动ID传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在日志记录中看到了许多其他问题.最佳做法.哪种日志记录平台是最好的.等等.这里有一些关于SO的链接,并对该主题进行了很好的讨论:

I have seen many other questions on logging. Best practices. What logging platform is best. Etc. Here are some links from here on SO with very good discussions on the topic:

记录最佳做法

log4net与TraceSource

的最佳日志记录解决方案. NET 3.5项目

.NET 3.5日志记录

开始

打了这么长时间的文章之后,我想我要弄清楚的主要事情是WCF日志记录/跟踪与活动ID传播与System.Diagnostics和TraceSources紧密关联的情况.您能否使用第三方日志记录平台(例如log4net或NLog)获得良好"的WCF日志记录/跟踪和活动ID传播.如果执行此操作,该怎么做?

Having typed this long post, I guess that the main thing that I am trying to figure out is how tightly coupled WCF logging/tracing and activity id propagation are to System.Diagnostics and TraceSources. Can you get "good" WCF logging/tracing and activity id propagation using a third party logging platform like log4net or NLog. If you do this, how do you do it?

有关ServiceTraceViewer的一些问题,请参见本文底部

See the bottom of this post for a few questions about ServiceTraceViewer,

结束编辑.

我在任何这些帖子中都没有详细讨论我的问题的主题.我对人们在日志记录和WCF方面的工作很感兴趣.如果您正在开发一个包含WCF服务的项目,并且已经登录了项目,则是否要做出特殊的努力来使用特定于WCF的日志记录功能.特别是,您是否尝试合并诸如活动跟踪,活动传播和端到端跟踪之类的内容?如MSDN上的这篇文章所述. 此处是MSDN上有关传播活动的另一篇文章.

The subject of my question is not discussed in much detail in any of these posts. I am interested in what people are doing regarding logging and WCF. If you are working on a project that includes WCF services and you have logging in your project, do you make any special efforts to use WCF-specific logging capabilities. In particular, do you try to incorporate things like Activity Tracing, activity propagation, and End to End tracing? Such as is outlined in this article from MSDN. Here is another article from MSDN on propagating activities.

这些文章很好地解释了如何使用System.Diagnostics TraceSources进行活动跟踪,活动传播和端到端跟踪.它显示了如何配置WCF以通过app.config/web.config文件打开"这些选项. WCF在内部使用TraceSources记录通信结果.

The articles do a pretty job of explaining how to do activity tracing, activity propagation, and end to end tracing using System.Diagnostics TraceSources. It shows how to configure WCF to "turn on" these options via the app.config/web.config file. WCF uses TraceSources internally to log the results of the communication.

这里有一些示例代码(来自上面链接的第二篇MSDN文章),或多或少显示了如何通过System.Diagnostics和TraceSources实现活动传播:

Here is some sample code (from the second MSDN article linked above) that shows more or less how to achieve activity propagation via System.Diagnostics and TraceSources:

TraceSource ts = new TraceSource("myUserTraceSource");
Guid oldID = Trace.CorrelationManager.ActivityId;
Guid traceID = Guid.NewGuid();
ts.TraceTransfer(0, "transfer", traceID);
Trace.CorrelationManager.ActivityId = traceID; // Trace is static
ts.TraceEvent(TraceEventType.Start, 0, "Add request");

double value1 = 100.00D;
double value2 = 15.99D;
ts.TraceInformation("Client sends message to Add " + value1 + ", " + value2);
double result = client.Add(value1, value2);
ts.TraceInformation("Client receives Add response '" + result + "'");

ts.TraceTransfer(0, "transfer", oldID);
ts.TraceEvent(TraceEventType.Stop, 0, "Add request");
Trace.CorrelationManager.ActivityId = oldID;

您可以通过以下方法从服务内部判断WCF是否传播了活动:

Here is a way that you can tell, from within a service, whether or not WCF has propagated an activity:

// Check if an activity was set in scope by WCF, i.e., if it was 
// propagated from the client. If not, i.e., ambient activity is 
// equal to Guid.Empty, create a new one.
if(Trace.CorrelationManager.ActivityId == Guid.Empty)
{
    Guid newGuid = Guid.NewGuid();
    Trace.CorrelationManager.ActivityId = newGuid;
}
// Emit your Start trace.
ts.TraceEvent(TraceEventType.Start, 0, "Add Activity");

// Emit the processing traces for that request.
serviceTs.TraceInformation("Service receives Add " 
                        + n1 + ", " + n2);
// double result = n1 + n2;
serviceTs.TraceInformation("Service sends Add result" + result);

// Emit the Stop trace and exit the method scope.
ts.TraceEvent(TraceEventType.Stop, 0, "Add Activity");
// return result;

从我所看到的所有示例中,都可以通过配置System.Service模型TraceSource(通常通过app.config)并将其propagationActivity属性设置为"true"来实现活动传播.实际上,通过在Trace.CorrelationManager.ActivityId上设置活动ID(guid)来传播活动.如果使用log4net或NLog,可以有效地使用WCF日志记录和活动传播吗?

From all of the examples that I have seen, activity propagation is achieved configuring (typically via app.config) the System.Service model TraceSource and setting its propagateActivity property to "true". Activities are actually propagated by setting the activity id (guid) on Trace.CorrelationManager.ActivityId. Can WCF logging and activity propagation be used effectively if you are using log4net or NLog?

我的项目将非常大量地使用WCF.我们目前正在尝试确定我们的日志记录解决方案.我认为我对WCF日志记录和活动传播如何与System.Diagnostics和TraceSources一起使用有很好的了解.我想更好地了解如何/是否可以使用log4net和NLog等日志记录平台实现类似的功能.

My project will be using WCF very heavily. We are currently trying to settle on our logging solution. I think that I have a pretty good understanding of how WCF logging and activity propagation works with System.Diagnostics and TraceSources. I would like to understand better how/if something similar can be achieved with logging platforms like log4net and NLog.

他们是否提供某些本地"支持?他们提供某种基础设施的可能性似乎更高一些,以便可以手动"实现活动传播.也许是这样的:

Do they provide some "native" support? It seems a little more likely that they provide some infrastructure so that the activity propagation could be achieved "manually". Maybe something like this:

//Inside client code:
ILog logger = LogManager.GetLogger("client");
Guid oldActivity = Trace.CorrelationManager.ActivityId;
if (oldActivity == Guid.Empty)
{
  Trace.CorrelationManager.ActivityId = Guid.NewGuid();
}

using (LogManager.NDC.Push(Trace.CorrelationManager.ActivityId))
{
  log.Info("Before calling WCF Service");

  wcfService.Method();

  log.Info("After calling WCF Service");
}
Trace.CorrelationManager.ActivityId = oldActivity;

如果将log4net/NLog日志记录格式配置为记录NDC堆栈的顶部,则客户端(当活动处于作用域内)记录的每条消息都将被标记"为活动ID.假设WCF服务的实现方式类似,则在服务调用期间记录的所有消息也将被记录(尽管可能在单独的文件中)并用相同的活动ID进行标记.因此,可以将服务"日志文件中的日志记录消息与客户端"日志中的相应消息相关联.

If the log4net/NLog logging format is configured to log the top of the NDC stack, then each message logged by the client (while the activity is in scope) will be "tagged" with the activity id. Assuming that the WCF service is implemented similarly, then all messages logged during the service call will also be logged (albeit, probably in a separate file) and tagged with the same activity id. So, it will be possible to correlate the logging messages in the "service" log file to the corresponding messages in the "client" log.

因此,如果您使用WCF并有日志记录,则有一些问题:

So, if you use WCF and you have logging, here are some questions:

  1. 您是否使用活动传播?
  2. 您是否使用TraceSources进行记录?
  3. 您是否使用其他日志记录平台(例如log4net,NLog)?
  4. 如果您使用其他日志记录平台,那么如何进行活动传播?
  5. 您是否混合使用第三方日志记录(log4net/NLog-用于大多数日志记录)和System.Diagnostics.TraceSource(用于WCF服务边界日志记录)?
  1. Do you use activity propagation?
  2. Do you use TraceSources for logging?
  3. Do you use some other logging platform (e.g. log4net, NLog)?
  4. If you use another logging platform, how do you do activity propagation?
  5. Do you use a mixture of third party logging (log4net/NLog - for most logging) and System.Diagnostics.TraceSource (for WCF service boundary logging)?

关于ServiceTraceViewer呢?你会用吗?我看到的大多数示例都显示了System.Diagnostics通过TraceSources和XmlTraceListener生成的输出.它可以消耗log4net,NLog等的输出吗?它在基于TraceSource的日志记录中是否最佳"工作?如果是这样,那么在WCF服务边界(捕获一些应用程序上下文以及WCF通信信息)仅包含少量基于TraceSource的日志记录以在ServiceTraceViewer中进行查看是否足够好"?作为我正在进行的WCF学习过程的一部分,我已经简短地使用了ServiceTraceViewer.

What about ServiceTraceViewer? Do you use it? Most examples I have seen show the output being generated by System.Diagnostics via TraceSources and the XmlTraceListener. Can it consume output from log4net, NLog, etc? Does it work "best" with TraceSource-based logging? If so, is it "good enough" to have just a little bit of TraceSource-based logging at the WCF service boundaries (capturing some app context as well as the WCF communication info) for viewing in ServiceTraceViewer? I have used ServiceTraceViewer briefly, just as part of my ongoing WCF learning process.

如果您走了这么远,谢谢您的阅读.也许我是在考虑日志记录,WCF活动传播以及在ServiceTraceViewer中查看日志的功能的整体集成.在选择日志记录平台和/或日志记录策略时,这似乎是一个重要的考虑因素,但是我对这些日志记录平台或WCF的经验不足,无法确定.

If you got this far, thanks for reading. Maybe I am overthinking the whole integration of logging, WCF activity propagation, and the ability to view logs in ServiceTraceViewer. It seems like an important consideration in the choice of logging platform and/or logging strategies, but I don't have enough experience with these logging platforms or WCF to know for sure.

推荐答案

我的价值不菲,我使用基于AOP的日志记录功能来编写/维护自己,但就像其他一些日志记录框架一样... Mine基于装饰器,但我可以将其扩展到调用堆栈上的所有内容.

Just my nickels worth, I use AOP based logging that I write/maintain myself, but it's like some of the other logging frameworks... Mine is based around decorators, but I can extend it to anything that gets on the callstack.

所以您有这样的地方:

using (LogManager.NDC.Push(Trace.CorrelationManager.ActivityId)) {
  log.Info("Before calling WCF Service");

  wcfService.Method();

  log.Info("After calling WCF Service");
}
Trace.CorrelationManager.ActivityId = oldActivity;

如果服务器上有类似的内容,那么我的方法在这方面可以工作,但是我的方法不适用于内部日志记录.我的被​​设置为执行此操作:

If you had something like this on the server then mine works in that regard, but my method doesn't work this way for internal logging. Mine is setup to do this:

[LogMethod( CaptureDirection = LoggingDirection.InOut /*Optional*/, CaptureVariables = Yes /*Optional*/ )]
public ClassName MyMethodName(params){
  //magic logging happens here on method entry

  DoSomething();

  //if you need logging here I can't do anything with my AOP system

  DoSomethingElse();


  //magic logging happens here on method exit
}

此外,您是否希望在客户端和服务器之间建立关联的日志记录?您如何协商这两个?您如何确保一个与另一个相关?

Additionally, are you looking to have correlated logging between the client and the server? How do you negotiate those two? How can you ensure that one is related to the other?

这篇关于使用log4net或NLog的WCF日志记录/跟踪和活动ID传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆