Esempio n. 1
0
        public static string GetLogDisplayName(this EventLogSession session, string logPath)
        {
            int capacity = 0x200, bufferUsed = 0, error = 0;
            var str         = logPath;
            var displayName = new StringBuilder(capacity);

            // Get handle
            var hEvt = IntPtr.Zero;

            if (SessionHandlePI == null)
            {
                var elhType = session?.GetType().Assembly.GetType("System.Diagnostics.Eventing.Reader.EventLogHandle");
                SessionHandlePI = session?.GetType().GetProperty("Handle", BindingFlags.Instance | BindingFlags.NonPublic, null, elhType, Type.EmptyTypes, null);
            }
            var o = SessionHandlePI?.GetValue(session, null);

            if (o != null)
            {
                hEvt = ((SafeHandle)o).DangerousGetHandle();
            }

            if (!EvtIntGetClassicLogDisplayName(hEvt, logPath, 0, 0, capacity, displayName, ref bufferUsed))
            {
                error = Marshal.GetLastWin32Error();
                if (error == 0x7a)
                {
                    displayName = new StringBuilder(bufferUsed);
                    capacity    = bufferUsed;
                    error       = 0;
                    if (!EvtIntGetClassicLogDisplayName(hEvt, logPath, 0, 0, capacity, displayName, ref bufferUsed))
                    {
                        throw new Win32Exception();
                    }
                }
            }

            if ((error == 0) && !string.IsNullOrEmpty(displayName.ToString()))
            {
                str = displayName.ToString();
            }
            return(str);
        }