Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
3.5k views
in Programming by 27 29 37
edited by

I got this error The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security. when writing an entry to an event log in event viewer using C# in SharePoint solution visual web part deployed as a farm solution.

The source was not found, but some or all event logs could not be searched

So I decided to ignore to check if the new custom source is already exist or not in event logs to avoid this error and tried to create it manually in Regedit at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application,

  1. I created a new Key as a custom source, then
  2. I created a new string with "EventMessageFile" as a name and "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\EventLogMessages.dll" as a value.

create a custom source in Registry using C#

The new custom source is created successfully and no need now to check if this custom source already exists in the event log or not!

so I used the below C# code to write an event entry in the event log directly as below

EventLog.CreateEventSource("WPLogs", "MyLogs");
EventLog eventLog = new EventLog();
eventLog.Source = "WPLogs";
eventLog.WriteEntry("Error", EventLogEntryType.Error, 1001);

but I still get this error "Cannot open log for source "SourceName". You may not have write access".

SharePoint C# Cannot open log for source. You may not have write access

My question is What're the required permissions to add an entry with a custom source in the event log using C# in the SharePoint solution?


1 Answer

1 like 0 dislike
by 163 204 403
selected by
 
Best answer

Solving: Cannot open log for source. You may not have write access

In your SharePoint solution, to avoid this error "Cannot open log for source. You may not have write access", you have to use SPSecurity.RunWithElevatedPrivileges to execute the specified method with Full Control rights even if the user does not otherwise have Full Control as the following:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    EventLog.CreateEventSource("WPLogs", "MyLogs");
    EventLog eventLog = new EventLog();
    eventLog.Source = "WPLogs";
    eventLog.WriteEntry("Error", EventLogEntryType.Error, 1001);
});

Note: for ASP.NET solutions, you have to disable and enable the impersonation using C# as mentioned at Disable ASP.NET Impersonate using C#
- Read also The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

If you don’t ask, the answer is always NO!
...