最大限度地减少事件记录的性能影响? [英] Minimize performance impact of event logging?

查看:184
本文介绍了最大限度地减少事件记录的性能影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用StringBuilder的发送字符串到日志之前来连接一串查询参数的静态记录功能。这个过程可以得到适度的长,因为我们可能有〜10个参数(.Append调用),并最终被〜200个字符长。

I have a static logging function that uses StringBuilder to concatenate a bunch of query parameters before sending the string to a log. This process can get moderately long, as we may have ~10 parameters (.Append calls) and end up being ~200 chars long.

我希望尽量减少记录功能的性能影响。 (此日志记录功能,可以称为每个Web请求多次,同时我们测量的处理时间为每个Web请求)

I want to minimize the performance impact of the logging function. (This logging function may be called multiple times per web request, and we measure the processing time for each web request)

如何/应当/我可以建立StringBuilders的池子,以提高性能?​​

How/Should/Can I build a "pool" of StringBuilders to improve performance?

我也能做到这一切的异步日志记录,对不对?我应该怎么办呢?

I can also do all this logging asynchronously, right? How should I do that?

推荐答案

由于记录通常状态依赖,把日志条目的实际建设小康到另一个线程通常不是一种选择。你可以,但是,获取相关数据以异步方式进行格式化。虽然我不会在这里实现整个日志机制(虽然你可以看到我的 ProcessQueue文章在$ C $的CProject 的东西,会更容易),你可以做这样的事情:

Since logging is usually state-dependent, putting the actual construction of the log entry off into another thread usually isn't an option. You could, however, capture the relevant data to format asynchronously. While I won't implement the entire logging mechanism here (though you can see my ProcessQueue article on CodeProject for something that will make it easier), you could do something like this:

public static void LogAsync<T1>(T1 value, Func<T1, string> formatter)
{
    // asynchronously call formatter(value) and log the result
}

public static void LogAsync<T1, T2>(T1 value1, T2 value2, Func<T1, T2, string> formatter)
{
    // asynchronously call formatter(value1, value2) and log the value
}

...and so on

如果字符串建设是什么市场影响了日志记录,那么(假设各种 T 类型是不可变的,或者至少是的通话之间切换,以 LogAsync 格式被调用),这应该减轻一点。

If the string construction is what's bogging down the logging, then (assuming that the various T types are immutable, or at least don't change between the call to LogAsync and when formatter is invoked) this should alleviate that.

这篇关于最大限度地减少事件记录的性能影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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