Question: Is it possible to export an event log in a process not "run as administrator"?
I'm following the example code at https://msdn.microsoft.com/en-us/library/bb671203(v=vs.90).aspx
using (var els = new EventLogSession())
{
els.ExportLogAndMessages("Security", PathType.LogName, "*", @"c:\temp\security.evtx");
}
This code runs successfully when I run the process using "run as administrator", but fails when not "run as administrator with the exception
System.UnauthorizedAccessException: "Attempted to perform an unauthorized operation."
Using similar code to access my application's event log
using (var els = new EventLogSession())
{
els.ExportLogAndMessages("MyAppLog", PathType.LogName, "*", @"c:\temp\myapplog.evtx");
}
I get similar results except the exception is different:
System.Diagnostics.Eventing.Reader.EventLogException: "The directory name is invalid"
Am I doing something wrong, or is there a different approach that will allow me to get an event log exported to an .evtx file without requiring admin privileges?
Notes:
- Under the hood, I believe this is calling (and failing) in the native method
EvtArchiveExportedLog
.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385232(v=vs.85).aspx - As such the following seems related: EvtArchiveExportedLog fails with ERROR_DIRECTORY , but I don't understand what I would need to do in order to make my situation work correctly.
Yes you can but only if you have the rights to access the event log you want to export.
However, your question seems to be more something like
Looking into the source code for
EventLogSession
you can see that the call toExportLogAndMessages
will call the native functionEvtExportLog
. If this function fails the error code is retrieved by callingGetLastError
. This native Windows error code is then mapped to one of several exceptions.The exception that you experience is thrown if any of the following errors happen:
If you specify a wrong event log name then ERROR_EVT_CHANNEL_NOT_FOUND is the error you encounter. My guess is that this is your problem.
However, you can call
EvtExportLog
yourself and inspect the native error code to better understand why the call fails:The native error code should give you a clear indication of what the underlying problem is.