当我尝试添加第二个线程(pp)时,我在EventViewer中收到错误。当我运行一个线程(dev)时,它工作正常。
protected override void OnStart(string[] args)
{
try
{
dev = new Thread(() => startRID("Dev"));
dev.IsBackground = true;
dev.Start();
pp = new Thread(() => startRID("PreProd"));
pp.IsBackground = true;
pp.Start();
}
catch (Exception ex)
{
//Log the exception
lock (eventLog)
{
eventLog.WriteEntry("Exception at OnStart: " + ex.Message, EventLogEntryType.Error, (int)EventId.InvalidApplicationSetting);
}
}
}
这是事件查看器中的错误消息:
Application: Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
at System.Diagnostics.EventLogInternal.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32, Int16, Byte[])
at System.Diagnostics.EventLog.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32)
at Service.Service.startRID(System.String)
at Service.Service.<OnStart>b__4_1()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
我之后还得到了一个应用程序错误:
Faulting application name: Service.exe, version: 1.0.0.0, time stamp: 0x5a736c00
Faulting module name: KERNELBASE.dll, version: 6.1.7601.23915, time stamp: 0x59b94f2a
Exception code: 0xe0434352
Fault offset: 0x000000000001a06d
Faulting process id: 0x2d0c0
Faulting application start time: 0x01d39b94c5b56337
Faulting application path: D:\SomeFolder\Debug\Service.exe
Faulting module path: C:\windows\system32\KERNELBASE.dll
Report Id: 09dabb6d-0788-11e8-8a16-0025b50b0a5d
这是startRID的样子。我添加了一堆catch并尝试将错误记录到事件查看器,但它没有记录它。
private void startRID(string farmName)
{
try
{
while (true)
{
if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[farmName].ConnectionString))
{
eventLog.WriteEntry(string.Format("Exception in startRID method - no connectionString string value for " + farmName), EventLogEntryType.Error,
(int)EventId.GeneralException);
return;
}
RedisInfoDaemon rid = new RedisInfoDaemon(farmName);
try
{
rid.Start();
}
catch (Exception ex)
{
eventLog.WriteEntry(string.Format("Exception in startRID method - ex " + ex.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
try
{
rid.Reset(false);
rid.Start();
}
catch (Exception err)
{
eventLog.WriteEntry(string.Format("Exception in startRID method inside- err " + ex.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
Console.Write(err.ToString());
rid.Reset(true);
Thread.Sleep(TimeSpan.FromMinutes(5));
rid.Start();
}
}
}
}
catch (Exception erer)
{
eventLog.WriteEntry(string.Format("Exception in startRID method - main - err " + erer.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
}
}
答案 0 :(得分:2)
我们无法看到startRID
的作用,因此我们无法告诉您爆炸的内容,但是:我们可以告诉您的是,不要让异常触及呼叫的顶部 - 堆栈,因为这会杀死你的应用程序。所以:也许可以添加一个方法:
static void SafelyDoSomething(string key) {
try { startRID(key); }
catch(Exception ex) {
// lots of logging here
Console.Error.WriteLine(ex); // add more than this!
}
}
并使用new Thread(() => SafelyDoSomething("Dev"))
/ new Thread(() => SafelyDoSomething("PreProd"));
等等。这至少可以让您知道出了什么问题。但最终:它在我们无法看到的代码中。重点:这不是修复 - 它是诊断工具。
答案 1 :(得分:1)
EventLog
not guaranteed to be thread-safe 。你将不得不重写你的逻辑。