working with worker roles is a little bit tricky if something goes wrong. I have exception occurring in my worker role, which forces role to quit and restart.
I decided to implement azure diagnostics solution to log what is going on. It works, when i try to write message to trace, but i am unable to catch exceptions and log them. I surround my code with try catch and try to write exception contents to trace.
Is this right approach? And why does it not work? Is there any better way to log exceptions in worker roles? Thank you. My code is:
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) is method which throws exception. More precisely, it is when i try to call SQL azure query. I know that diagnostics works, because when role starts, it calls Trace.TraceInformation("Starting Worker Role TrackProcessor"); and it is visible in the table storage as mesage. Then over time more messages occur, as role encounters exception and is forced to restart. However, no error messages in log.