-1

Probably I just misunderstand System.Diagnostics.EventLog, but I am not able to write to EventLog under my app name. Project is ASP.NET web service (.NET Framework 4) and works under NETWORK SERVICE user.

I tried this and it works (as metioned here):

using (System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog())
{
    eventLog.Source = "Application";
    eventLog.WriteEntry("MY LOG INFO", System.Diagnostics.EventLogEntryType.Information);
}

but Application is not name of my application. So I tried to change eventLog.Source = "Application"; into eventLog.Source = System.Diagnostics.Process.GetCurrentProcess().ProcessName;. Result is exception The source was not found, but some... as here. ProcessName is w3wp, it is OK for me, but I cannot write log. So I tried overload constructor with logName parameter, but unsuccessfully.

If I understand well, WriteLog method in some cases need access registry (answer here)? OK, this need more then update of dll. Great suggestion is write mentioned registry change in installing process. However my change is "only" update in distributed dll, not new installation.

MSDB is mean, I find something about EventLogPermissionAttribute(here) and EventLogPermission(here), but it doesn't help me at all. I tried use it, but always I get same exception. There are no examples.

Last I find this (source):

EventLogPermission eventLogPermission = new EventLogPermission(EventLogPermissionAccess.Administer, ".");
eventLogPermission.PermitOnly();
if (!EventLog.SourceExists(source))
{
    EventLog.CreateEventSource(source, "Application");
}
EventLog.WriteEntry(source, message, entryType);

But it ends in the similar exception. Now It says little detail: it cannot access "Security" protocol.

So, question is: How should I write to EventLog? Or it is really necessary to update registry (I can do it via our updating tool, but I am not persuaded I want do this).

I also would like to understand how System.Diagnostics.EventLog works and what it is doing on background for better understanding of my code (if it will be usable for me).

Vít Bednář
  • 288
  • 2
  • 10

1 Answers1

0

You need Administrator access to create a custom event log source, and NETWORK SERVICE doesn't have that access. You will need to update your installer to create that log source, then the installer will have to be run as Administrator.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • This confirms my research. Finally, I chose a different approach. It is easier write log to files where I have access. Thanks – Vít Bednář Apr 12 '21 at 10:57