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

前端 未结 10 2174
栀梦
栀梦 2020-12-01 02:12

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

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 03:14

    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');
    

提交回复
热议问题