简体   繁体   English

为错误写入事件日志

[英]Write-EventLog for errors

When running the following one liner:运行以下一个班轮时:

Write-EventLog -LogName Application -Source 'Application Error' -EntryType Error -EventID 1001 -Message 'Problem description'

We see the entry in the log Application :我们在日志Application看到条目:

在此处输入图片说明

According to Microsoft for EventID 1001, one should provide the values for %1, %2 and %3:根据Microsoft的 EventID 1001,应该提供 %1、%2 和 %3 的值:

Detection of product '%1', feature '%2' failed during request for component '%3'在请求组件 '%3' 期间检测到产品 '%1',功能 '%2' 失败

How is this possible in PowerShell?这在 PowerShell 中怎么可能? When adding the switch -RawData 10, 20 only the type is filled in as following:添加开关-RawData 10, 20只填写类型如下:

在此处输入图片说明

Is there a way to not have the other text available without creating a new log name or source in the Event log?有没有办法让其他文本不可用而无需在事件日志中创建新的日志名称或源? Or to be able to fill in the variables?或者能够填写变量? I'm writing to Application error in case the custom made log name or source isn't available.如果自定义日志名称或源不可用,我正在写Application error So there is somewhere a trace.所以在某处有痕迹。

Thank you for your help.感谢您的帮助。

The Event ID 1001 description you link to is specific to the MsiInstaller source.您链接到的事件 ID 1001 描述特定于MsiInstaller源。

For your own custom error events, use a custom Source identifier.对于您自己的自定义错误事件,请使用自定义源标识符。 You can check whether a source definition already exists on the machine, and if not, create it:您可以检查机器上是否已存在源定义,如果不存在,则创建它:

$CustomSource = 'MyCustomApplication'

# Wrap check i try/false to catch SourceExists() throwing access denied on failure
$SourceExists = try {
    [System.Diagnostics.EventLog]::SourceExists($CustomSource)
} catch {
    $false
}

if(-not $SourceExists)
{
    # Create the Source definition
    New-EventLog -Source $CustomSource -LogName Application
}

Write-EventLog -LogName Application -Source $CustomSource -EntryType Error -EventID 1001 -Message 'Problem description'

If you have a message resource file (the file containing the "templates" for your events), you can also include that in the call to New-EventLog如果您有消息资源文件(包含事件“模板”的文件),您还可以将其包含在对New-EventLog的调用中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM