【问题标题】:Elevate user to Admin level to write to Eventlog in .NET将用户提升到管理员级别以写入 .NET 中的事件日志
【发布时间】:2012-07-06 05:34:15
【问题描述】:

我正在开发一个 Windows .NET 应用程序,我想写入事件日志。

Public Shared Sub WriteExceptionToEventLog(ByVal message As String)
        Dim cs As String = "TESTLOG"
        Dim elog As New EventLog()
        Dim sourceExist As Boolean

        Try
            sourceExist = EventLog.SourceExists(cs)
        Catch ex As Exception
            sourceExist = False
        End Try

        If Not sourceExist Then
            Dim ev As New EventLogPermission(EventLogPermissionAccess.Administer, ".")
            ev.PermitOnly()
            EventLog.CreateEventSource(cs, "TESTLOG")
        End If
        elog.Source = cs
        elog.EnableRaisingEvents = True
        EventLog.WriteEntry(cs, message, EventLogEntryType.[Error])

    End Sub

但这不起作用,因为 Windows 7 中的用户需要管理员权限才能写入事件日志。当我以“Run ad Admin”模式执行应用程序时,同样成功。

那么有什么方法可以为 vb.net 中的代码段授予管理员权限(模拟除外)?

【问题讨论】:

  • 您是在常规应用程序还是服务中运行?
  • 它是一个普通的 Windows 应用程序.. 不是服务

标签: .net vb.net windows-7 event-log securityexception


【解决方案1】:

您只需要管理员权限来创建事件源而不是写入它。

在安装时或在提升的命令提示符中手动创建源。

eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO mysource /D "created mysource"

【讨论】:

  • 感谢 adrianm,但我在一个解决方案中有几个小应用程序,并希望使用代码动态创建事件源
  • 那么您需要以管理员权限运行应用程序或更改安全策略。为什么不让您的所有应用程序使用相同的源?
【解决方案2】:

您可以将 app.manifest requestedExecutionLevel 更改为 requireAdministrator - 这将在应用程序运行时强制出现 UAC 提示,并且该应用程序只有在可以以管理员身份运行时才会运行。 (要更改此设置,请转到 Project Properties>Application tab>View Windows Settings

如果您的应用程序经常需要管理员权限,那么这确实是唯一的方法。

如果您只是有时需要管理员权限,那么您可以在需要写入事件日志时以更高的权限重新启动应用程序。请查看this informative article,了解有关在 .NET 应用程序中使用 UAC 的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2012-09-12
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多