問題描述
IOException 寫入事件日誌 (IOException writing to event log)
我們今天遇到了這個異常,每次我們重試導致它的操作時都會重複
System.IO.IOException: Insufficient system resources exist to complete the requested service.
at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value, RegistryValueKind valueKind)
at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)
at System.Diagnostics.EventLog.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String source, String message)
at DoWork()
重新啟動為我們解決了這個問題,但沒有解釋根本原因。我們通常編寫少於 20 個字符的源代碼和少於 100 個字符的消息。運行代碼的機器上的資源使用情況似乎很好。
參考解法
方法 1:
I dug in a little deeper after the reboot was done and got a copy of the application log from the event viewer. There were ~66,000 events for that day only. I wrote a test app to write 66,000 unique messages to the log and it completed pretty quickly.
Then I noticed we had a different source for every 10 or so messages so I changed my test app to write a unique source and message every time. After about 15 minutes it only wrote 6000.
Though I wasn't able to reproduce the exception in the question I believe our problem was that we were creating new EventSources needlessly and often which goes against the recommended way of writing to the event log.
As an addendum, if the sources already existed, it wouldn't be nearly as big an issue