When I write a log into windows event log, I get the event below, what\'s the root cause of this message, and how can I fix it? Many Thanks
The descri
You need to create an event source and a message file for it. Code looks something like this:
var data = new EventSourceCreationData("yourApp", "Application");
data.MessageResourceFile = pathToYourMessageFile;
EventLog.CreateEventSource(data);
Then you will need to create a message file. There is also this article that explains things (I did not read it all but it seems fairly complete).
Restart your system!
A friend of mine had exactly the same problem. He tried all the described options but nothing seemed to work. After many studies, also of Microsoft's description, he concluded to restart the system. It worked!!
It seems that the operating system does not in all cases refresh the list of registered event sources. Only after a restart you can be sure the event sources are registered properly.
Improving on the answer by @Alex, I suggest the following:
using (EventLog eventLog = new EventLog("Application"))
{
//You cannot be sure if the current identity has permissions to register the event source.
try
{
if (System.Web.HttpRuntime.AppDomainAppId != null)
{
eventLog.Source = System.Web.HttpRuntime.AppDomainAppId;
}
else
{
eventLog.Source = Process.GetCurrentProcess().ProcessName;
}
}
catch (SecurityException)
{
eventLog.Source = "Application";
}
eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 1000);
}
It is important here not to specify category
parameter. If you do, and this is the same for the .NET Runtime
so-called magic, the
The description for Event ID <...> from source <...> cannot be found.
is going to appear.
Use PowerShell to create your event log and source:
New-EventLog -LogName MyApplicationLog `
-Source MySource `
-MessageResourceFile C:\windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll
You'll need the messages dll to avoid the problem you are seeing.
How about a real world solution.
If all you need is a "quick and dirty" way to write something to the event log without registering "custom sources" (requires admin rights), or providing "message files" (requires work and headache) just do this:
EventLog.WriteEntry(
".NET Runtime", //magic
"Your error message goes here!!",
EventLogEntryType.Warning,
1000); //magic
This way you'll be writing to an existing "Application" log without the annoying "The description for Event ID 0 cannot be found"
If you want the "magic" part explained I blogged about it here
For me, the problem was that my target profile by accident got set to ".Net Framework 4 Client profile". When I rebuilt the service in question using the ".Net Framework 4", the problem went away!