首页 文章

Azure Worker角色错误处理

提问于
浏览
2

如果出现问题,与 Worker 角色合作会有点棘手 . 我的工作角色发生异常,强制角色退出并重新启动 .

我决定实施azure诊断解决方案来记录正在发生的事情 . 它工作,当我尝试写消息跟踪,但我无法捕获异常并记录它们 . 我用try catch包围我的代码,并尝试编写异常内容以进行跟踪 .

这是正确的方法吗?为什么它不起作用?有没有更好的方法来记录工作者角色中的异常?谢谢 . 我的代码是:

public class WorkerRole : RoleEntryPoint
    {

    private const int WAIT_INTERVAL_SECONDS = 15;

    public override void Run()
    {
        Trace.WriteLine("$projectname$ entry point called", "Information");

        while (true)
        {

            try
            {
                string id = MainWCFRole.Storage.TrackProcessingQueueDAO.retrieveMsgContents();
                if ((id != null) && (!id.Equals("")))
                {
                    var points = MainWCFRole.Storage.TrackRawDataDAO.retrieve(id);

                    Processor.process(id, points);
                }
                else
                {
                    Thread.Sleep(WAIT_INTERVAL_SECONDS * 1000);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceInformation("Something went wrong");
                Trace.TraceError(ex.ToString());
                throw ex;
            }


        }
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
        dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
        dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
        dmc.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
        dmc.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);


        DiagnosticMonitor.Start("StorageConnectionString", dmc);

        Trace.TraceInformation("Starting Worker Role TrackProcessor");

        return base.OnStart();
    }

}

Processor.process(id,points)是抛出异常的方法 . 更准确地说,当我尝试调用SQL azure查询时 . 我知道诊断工作正常,因为当角色启动时,它会调用Trace.TraceInformation(“Starting Worker Role TrackProcessor”);它在表存储中可见为消息 . 然后随着时间的推移会发生更多消息,因为角色遇到异常并被迫重启 . 但是,日志中没有错误消息 .

2 回答

  • 0

    如果Run()方法退出,那么您的工作程序将被回收 . 在回收之前,工作人员可能没有时间将日志传输到存储 .

    最简单的解决方案是在捕获异常后不再重新抛出异常 . 然后,您的辅助角色将继续运行,直到手动关闭为止 .

  • 3

    你是否为工作者角色连接了Trace Listener?看看:http://blogs.msdn.com/b/jimoneil/archive/2010/10/08/azure-home-part-8-worker-role-and-azure-diagnostics.aspx

    工作者角色app.config .

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.diagnostics>
            <trace>
                <listeners>
                    <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener,                            Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0,                            Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                        name="AzureDiagnostics">
                        <filter type="" />
                    </add>
                </listeners>
            </trace>
        </system.diagnostics>
    </configuration>
    

相关问题