在“应用程序和设置日志”下的子目录中创建事件日志

问题描述 投票:1回答:1

我一直在寻找一种方法来创建一个在应用程序和服务日志的子目录下创建许多单独的事件日志的方法,就像有一个子目录Microsoft然后它有一个子目录Windows然后各种其他目录一样随着应用程序登录

\ Applications和Services \ Microsoft \ Windows \ All-User-Install-Agents \ Applications and Services \ Microsoft \ Windows \ AppHost ...

我想创建类似以下的东西

\应用和服务\我的公司\应用1 \应用和服务\我的公司\应用2 \应用和服务\我的公司\应用3

我遇到的所有示例只允许您直接在\ Applications和Services目录下创建日志,而不是创建子目录。

谢谢

c# event-log windows-server-2012-r2
1个回答
-1
投票

我正努力让子文件夹也能正常工作,因为我希望有一个类似的结构:

- Application and Services Logs
-- Company Name
--- Application 1
---- ApplicationLog
--- Application 2
---- SecurityLog
---- OperationalLog

我找不到任何方法直接使用C或PowerShell执行此操作,但在使用注册表项和https://docs.microsoft.com/en-us/windows/desktop/eventlog/eventlog-key提供的文档进行一些试验和错误后,我终于得到了它的工作。

您似乎需要在HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ WINEVT \ Channels中创建密钥,其中主注册表项名称是“文件夹”结构的关键。 a' - '被视为更深层次的结构。例如:CompanyName \ Application \ Log,应该是名为CompanyName-Application-Log的键。

下面是使用PowerShell执行此操作的示例脚本:

# Create the eventlog (in a subfolder structure)
# Params()
$PrimaryEventKey = 'Company'
$ApplicationName = 'Application'
$LogName = 'NewLog'

# Vars()
$primarylocation = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\WINEVT\Channels'
$LogName = $PrimaryEventKey + '-' + $ApplicationName + '-' + $LogName
$EventRoot = (Join-Path $primarylocation $LogName)

if (!(Test-Path $EventRoot)) {
New-Item -Path ($secondarylocation + '\' + $Logname)
New-ItemProperty -Path ($secondarylocation + '\' + $Logname) -Name providerGuid -PropertyType String -Value "{$($GUID)}"

New-Item -Path $EventRoot
New-ItemProperty -Path $EventRoot -Name Enabled -PropertyType DWord -Value 1
New-ItemProperty -Path $EventRoot -Name Type -PropertyType DWord -Value 1
New-ItemProperty -Path $EventRoot -Name Isolation -PropertyType DWord -Value 0
New-ItemProperty -Path $EventRoot -Name RestrictGuestAccess -PropertyType String -Value 1
New-ItemProperty -Path $EventRoot -Name OwningPublisher -PropertyType String -Value "{$($GUID)}"

    # See https://docs.microsoft.com/en-us/windows/desktop/eventlog/eventlog-key for documentation on the ChannelAccess or or RestrictGuestAccess (see: RestrictGuestAccess / Isolation)
}
else {
    Write-Warning 'Event Log (Key) Already exists in registry'
}

# Write into the event log (Example)
$eventType = ([System.Diagnostics.EventLogEntryType]::Information)
$evt = New-Object System.Diagnostics.EventLog($LogName)
$evt.Source = "SomeSource"
$evt.WriteEntry("random message", $eventType, 60001)
© www.soinside.com 2019 - 2024. All rights reserved.