请问Task.Factory.StartNew有任何性能副作用? [英] Does Task.Factory.StartNew has any performance side effects?

查看:1852
本文介绍了请问Task.Factory.StartNew有任何性能副作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我赶上和记录异常在ASP.NET Web API 2应用程序通过启动一个新的线程来登录:

 无效MainMethod(){
    尝试{
        // 东东...
    }赶上(例外前){
       Logger.Log(除息);
    }
}类记录器{
    无效日志(异常前){
        Task.Factory.StartNew(()=> LogAsync(前));
    }
    无效LogAsync(异常前){
        //做一些反思的东西来获取code-地方
        //保存日志...
    }
}

现在的问题是:在一个真正的高流量的应用程序,做这种做法有没有副作用?它是更好地登录的主要方面?

P.S。现在的问题是有关这个


解决方案

  

在一个真正的高流量的应用程序,做这种做法有什么副作用?


这通常取决于你正在运行的环境,你的硬件是什么,以及真正的高流量,其实就是。

在一般情况下,使用一个新的线程来只需登录可能会产生比释放当前线程做更多的工作实际获得更多的开销。是的,它会使用由ASP.NET线程池使用相同的线程,如果它被频繁调用,你可能会导致饥饿到您的池,尽管这是不可能的。

更多了,使用 Task.Factory.StartNew 是的 ASP.NET是危险的,因为它没有注册卸载到与IIS该线程的工作。

海事组织,你应该保持它的简单,同步记录。

I'm catching and logging exceptions in an ASP.NET Web API 2 app by starting a new thread to log:

void MainMethod(){
    try{
        // stuff...
    } catch (Exception ex) {
       Logger.Log(ex);
    }
}

class Logger {
    void Log(Exception ex) {
        Task.Factory.StartNew(() => LogAsync(ex));
    }
    void LogAsync(Exception ex) {
        // doing some reflection stuff to retrieve code-place
        // saving the log...
    }
}

The question is: in a real high traffic app, does this approach has any side effects? Is it better to log in the main context?

P.S. The question is related to this one.

解决方案

in a real high traffic app, does this approach has any side effects?

That will usually depend on the environment you're running in, what your hardware is, and what "real high traffic" actually means.

In general, using a new thread to simply log might produce more overhead than the actual gain of "freeing up" the current thread to do more work. Yes, it will use the same threads used by the ASP.NET ThreadPool, and if it gets called frequently, you might be causing starvation to your pool, although that is unlikely.

More over, using Task.Factory.StartNew is ASP.NET is dangerous because it doesn't register the work offloaded to that thread with IIS.

IMO, You should keep it simple and log synchronously.

这篇关于请问Task.Factory.StartNew有任何性能副作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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