I am getting the following exception. I have given full control to Asp.net account on Eventlogs in Registry edit.
[SecurityException: The source was not
Didnt work for me.
I created a new key and string value and managed to get it working
Key= HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application\<Your app name>\
String EventMessageFile value=C:\Windows\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll
If you just want to sniff if a Source exists on the local machine but don't have ability to get authorization to do this, you can finger it through the following example (VB).
This bypasses the security error. You could similarly modify this function to return the LogName for the Source.
Public Shared Function eventLogSourceExists(sSource as String) as Boolean
Try
EventLog.LogNameFromSourceName(sSource, ".")
Return True
Catch
Return False
End Try
End Function
Inaccessible logs: Security
A new event source needs to have a unique name across all logs including Security (which needs admin privilege when it's being read).
So your app will need admin privilege to create a source. But that's probably an overkill.
I wrote this powershell script to create the event source at will. Save it as *.ps1
and run it with any privilege and it will elevate itself.
# CHECK OR RUN AS ADMIN
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# CHECK FOR EXISTENCE OR CREATE
$source = "My Service Event Source";
$logname = "Application";
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {
[System.Diagnostics.EventLog]::CreateEventSource($source, $logname);
Write-Host $source -f white -nonewline; Write-Host " successfully added." -f green;
}
else
{
Write-Host $source -f white -nonewline; Write-Host " already exists.";
}
# DONE
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
I recently experienced the error, and none of the solutions worked for me. What resolved the error for me was adding the Application pool user to the Power Users group in computer management. I couldn't use the Administrator group due to a company policy.