如何实例C#TraceSources登录(多线程)ASP.NET 2.0 Web应用程序(ASMX WS)? [英] how to instantiate C# TraceSources to log (multithreaded) ASP.NET 2.0 Web application (asmx ws)?

查看:191
本文介绍了如何实例C#TraceSources登录(多线程)ASP.NET 2.0 Web应用程序(ASMX WS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TraceSource的例子,我发现地址多线程无。我创建一个类(SyncService)的新实例服务于传入请求。当有大量的活动,有需要使用此TraceSource登录活动多线程。我应该如何使用这个类在考虑性能和线程安全登录?我有一些OPTS记:

None of the examples on TraceSource I found addresses multithreading. I create new instance of a class (SyncService) to serve a incoming request. When there is lots of activity there is multiple threads that need to use this TraceSource to log activities. How should I use this class for logging with performance and thread safety in mind? I have some opts in mind:


    private static TraceSource _source;

    static SyncService()
    {
        _source = new TraceSource("mySrc");
    }

的情况下,该线程安全上面是一个问题。有相当多的记录,所以我不想让任何地方的锁来保护访问TraceEvent法。怎样去除静态的关键字?有很多的开销时,所有的请求都从自己TraceSource登录?难道还有比这2更好的解决办法? MSDN规定:任何公共静态这种类型的成员(在Visual Basic中的Shared)是线程安全的所有实例成员都不能保证线程安全的。我认为关于应该怎样?

in case above the thread safety is an issue. There is quite a lot logging so i don't want to make locks everywhere to guard access to TraceEvent-method. What about removing both static-keywords? Is there lots of overhead when all the requests do logging from their own TraceSource? Is there better solution than these 2? msdn states: "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." What should i thinks about that?

推荐答案

定义TraceSource这样,以确保您只得到一个实例:

Define TraceSource like this to ensure you only get one instance:

private static readonly TraceSource _source = new TraceSource("mySrc");

这将确保它创建的任何线程开始使用它之前,所以你只有一个实例保证。

This will make sure it is created before any thread starts using it, so you are guaranteed one instance only.

TraceSource都有默认的Trace.UseGlobalLock全局锁。这将自动锁定你周围所有的跟踪侦听器。如果设置为false,将每个TraceListener的锁如果听者没有被定义为线程安全的吧。

TraceSource has default a global lock in Trace.UseGlobalLock. This will automatically lock for you around all trace listeners. If set to false it will lock per TraceListener if the listener is not defined as thread safe instead.

跟踪可以是一个瓶子颈部如果记录速度是慢和跟踪的体积是大的。

Tracing can be a bottle neck if the recording speed is "slow" and the volume of tracing is large.

要确保最大速度,你需要使用尽可能少的跟踪侦听器成为可能,确保它们是线程安全的,并设置Trace.UseGlobalLock = FALSE。如果您没有在您的app.config添加了听众的控制,你应该保持全局​​锁。

To ensure max speed you need to use as few trace listeners as possible, make sure they are thread safe, and set Trace.UseGlobalLock = false. If you don't have control over listeners being added in your app.config you should keep the global lock.

这篇关于如何实例C#TraceSources登录(多线程)ASP.NET 2.0 Web应用程序(ASMX WS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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