记录到自定义事件日志(C# 应用程序,但使用 win32 API)

发布于 2024-07-19 05:16:19 字数 3494 浏览 3 评论 0原文

由于 .NET EventLog 类的限制,我有一些使用 PInvoke 的代码记录到应用程序日志。 该代码运行没有问题。

但现在,我想记录到自定义事件日志。 因此,我尝试将 RegisterEventSource 的第二个参数更改为“companyX”(我的自定义日志)。 它正确写入“companyX”(而不是应用程序日志),但它还将源字段(单个日志条目的)设置为“companyX”,即不是我想要的。

那么我如何修改代码以:

  • 使用新的自定义事件日志(“companyX”),同时也
  • 使用正确的源值(“MyApp”)

目前,它要么正确写入应用程序日志(使用正确的源值) “MyApp”),或者写入自定义(“companyX”)事件日志,并使用不正确源值(而不是“MyApp”,它将源值设置为“companyX”,自定义日志的名称)。

编辑:请注意,“companyX”日志已创建(在 WIX 安装程序代码中)。

这是我的代码:

private void WriteEntryToLog(string msg, LogEventType entryType)
{
    // ApplicationName = "MyApp"
    // The code as-is is writing to the Application log. Changing the 2nd param of
    // the function below to "companyX" allows me to write to my "companyX" event log,
    // but also changes the Source value of each entry append to that log to "companyX" rather than "MyApp"
    IntPtr eventSrcHandle = NativeMethods.RegisterEventSource(null, Resources.ApplicationName);


    try
    {
        uint tokenInfoSize = 0;
        IntPtr userTokenPtr = WindowsIdentity.GetCurrent().Token;
        const UInt16 typeError = 1, typeWarning = 2, typeInformation = 4;

        //using this first call, get the length required to hold the token information in the tokenInfoSize parameter
        bool bresult = NativeMethods.GetTokenInformation(userTokenPtr, NativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, tokenInfoSize, out tokenInfoSize);
        if (bresult) throw new Win32Exception(Marshal.GetLastWin32Error());

        IntPtr userTokenInfo = Marshal.AllocHGlobal((int)tokenInfoSize);
        try
        {
            //get the user token now with the pointer allocated to the right size
            bresult = NativeMethods.GetTokenInformation(userTokenPtr, NativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, userTokenInfo, tokenInfoSize, out tokenInfoSize);
            if (!bresult) throw new Win32Exception(Marshal.GetLastWin32Error());

            UInt16 type = typeError;
            switch (entryType)
            {
                case LogEventType.Error:
                    type = typeError; 
                    break;
                case LogEventType.Warning:
                    type = typeWarning; 
                    break;
                case LogEventType.Information:
                    type = typeInformation; 
                    break;
                    default: 
                        type = typeInformation; 
                        break;
            }

            NativeMethods.TOKEN_USER tokUser = (NativeMethods.TOKEN_USER)Marshal.PtrToStructure(userTokenInfo, typeof(NativeMethods.TOKEN_USER));

            string[] message = new string[1];
            message[0] = msg;
            bresult = NativeMethods.ReportEvent(eventSrcHandle, type, 0, 0, tokUser.User.Sid, 1, 0, message, new byte());
            if (!bresult) throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        finally
        {
            Marshal.FreeHGlobal(userTokenInfo);
        }
    }
    finally
    {
        NativeMethods.DeregisterEventSource(eventSrcHandle);
    }

}

哦,这不是重复这里的问题: ASP.NET 事件日志源属性的自定义值错误 因为我必须为此使用 PInvoke。 =)

Due to a limitation in the .NET EventLog class, I have some code using PInvoke that logs to the Application log. The code works without a problem.

But now, I'd like to log to a custom event log. So, I tried changing the 2nd parameter of the RegisterEventSource to "companyX" (my custom log). It correctly wrote to "companyX" (instead of the Application log), but it also set the Source field (of the individual log entry) to "companyX," which is not what I want.

So how do I modify the code to:

  • Use the new, custom event log ("companyX"), but also
  • Use the correct Source value ("MyApp")

Currently, it either correctly writes to the Application log (with the correct Source value of "MyApp"), or it writes to the custom ("companyX") event log, and uses the incorrect Source value (rather than "MyApp", it sets the Source value to "companyX", the name of the custom log).

EDIT: Note that the "companyX" log has already been created (in WIX installer code).

Here's my code:

private void WriteEntryToLog(string msg, LogEventType entryType)
{
    // ApplicationName = "MyApp"
    // The code as-is is writing to the Application log. Changing the 2nd param of
    // the function below to "companyX" allows me to write to my "companyX" event log,
    // but also changes the Source value of each entry append to that log to "companyX" rather than "MyApp"
    IntPtr eventSrcHandle = NativeMethods.RegisterEventSource(null, Resources.ApplicationName);


    try
    {
        uint tokenInfoSize = 0;
        IntPtr userTokenPtr = WindowsIdentity.GetCurrent().Token;
        const UInt16 typeError = 1, typeWarning = 2, typeInformation = 4;

        //using this first call, get the length required to hold the token information in the tokenInfoSize parameter
        bool bresult = NativeMethods.GetTokenInformation(userTokenPtr, NativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, tokenInfoSize, out tokenInfoSize);
        if (bresult) throw new Win32Exception(Marshal.GetLastWin32Error());

        IntPtr userTokenInfo = Marshal.AllocHGlobal((int)tokenInfoSize);
        try
        {
            //get the user token now with the pointer allocated to the right size
            bresult = NativeMethods.GetTokenInformation(userTokenPtr, NativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, userTokenInfo, tokenInfoSize, out tokenInfoSize);
            if (!bresult) throw new Win32Exception(Marshal.GetLastWin32Error());

            UInt16 type = typeError;
            switch (entryType)
            {
                case LogEventType.Error:
                    type = typeError; 
                    break;
                case LogEventType.Warning:
                    type = typeWarning; 
                    break;
                case LogEventType.Information:
                    type = typeInformation; 
                    break;
                    default: 
                        type = typeInformation; 
                        break;
            }

            NativeMethods.TOKEN_USER tokUser = (NativeMethods.TOKEN_USER)Marshal.PtrToStructure(userTokenInfo, typeof(NativeMethods.TOKEN_USER));

            string[] message = new string[1];
            message[0] = msg;
            bresult = NativeMethods.ReportEvent(eventSrcHandle, type, 0, 0, tokUser.User.Sid, 1, 0, message, new byte());
            if (!bresult) throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        finally
        {
            Marshal.FreeHGlobal(userTokenInfo);
        }
    }
    finally
    {
        NativeMethods.DeregisterEventSource(eventSrcHandle);
    }

}

Oh, and this is not a repeat of the question here:
Custom value for the Event Log source property for ASP.NET errors
since I have to use PInvoke for this. =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2024-07-26 05:16:19

您必须创建一个名为 MyApp 的源并将其映射到您的日志“CompanyX”。

本文详细介绍了如何使用 .Net Framework BCL 创建事件源。

http://msdn.microsoft.com/en-us/library/5zbwd3s3。 aspx

此更改需要更新对注册表的访问权限。

You have to create a source called MyApp and map it to your log "CompanyX".

This article goes into detail for creating an Event Source w/ .Net Framework BCL.

http://msdn.microsoft.com/en-us/library/5zbwd3s3.aspx

This change requires update access to registry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文