我正在尝试在 .net 中设置基本日志记录到 .net 中的 Windows 事件日志System.Diagnostics.EventLog,但我没有看到任何实际写入日志的事件。考虑以下代码:
// Elsewhere in the class
private static readonly string EventLogName = "LogName";
private static readonly string EventLogSource = "AppName";
// In the only function that does something
if (!EventLog.Exists(EventLogName))
{
EventLog.CreateEventSource(EventLogSource, EventLogName);
return;
}
else
{
Trace.TraceInformation("Attempting log");
// This doesn't write anything
EventLog.WriteEntry(EventLogSource,
"StaticWriteEntry",
EventLogEntryType.Error);
// Neither does this
using (var log = new EventLog())
{
log.Log = EventLogName;
log.Source = EventLogSource;
log.WriteEntry("WriteEntry?", EventLogEntryType.Error);
}
}
return;
Run Code Online (Sandbox Code Playgroud)
根据 MSDN 示例,我第一次创建日志并退出应用程序。当然,此日志创建最终将进入设置。后续运行尝试将消息记录到创建的事件日志中。 …
在 Windows Server 2008 和 Windows 7 中,“应用程序和服务日志”下有分类的新事件。还有一个名为 Microsoft 的子文件夹,其中也包含大量子文件夹。
有没有办法通过WMI收集这些事件?对于常规“Windows 日志”(例如应用程序和安全性),可以使用 cimv2 命名空间中的 Win32_NTLogEvent WMI 类。但是,此类不提供对新 Microsoft 事件日志的访问。
有任何想法吗?
我编写了一个简短的程序,使用启动和关闭时发布的事件日志消息来确定远程 PC 的正常运行时间。目前的逻辑是:
foreach (eventlogentry)
{
if (entryTime > OldestTime)
{
if (entry = Startup)
{
addOnTime(entry.Time);
}
if (entry = Shutdown)
{
addOffTime(entry.Time);
}
}
}
Run Code Online (Sandbox Code Playgroud)
“OldestTime”定义向后扫描多远的时间......
我想知道是否有任何方法可以轻松修改我的程序以从最新到最旧读取事件?
它正在读取远程事件日志,并且运行此函数需要一段时间,因为它从最后开始并向前读取。
我知道这一点是因为我在第一个“if”块中添加了一个“else”块以跳出 foreach 块,如果条目不在我们正在寻找的时间跨度内并且程序在它读取的第一个事件处停止。
我创建了一个 Windows 服务。我创建一个事件日志。
public Service1()
{
InitializeComponent();
this.ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");
string sourceName = ConfigurationManager.AppSettings.Get("ServiceName");
string logName = ConfigurationManager.AppSettings["EventLogName"];
try
{
if (!System.Diagnostics.EventLog.Exists(sourceName))
System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
eventLog.Source = sourceName;
eventLog.Log = logName;
}
catch
{
eventLog.Source = "Application";
}
}
Run Code Online (Sandbox Code Playgroud)
初始化期间,会安装服务但不会创建日志。日志条目位于Application系统日志中。
我错过了什么?
我使用进程安装程序进行安装
public ProjectInstaller()
{
InitializeComponent();
this.Installers.Add(GetServiceInstaller());
this.Installers.Add(GetServiceProcessInstaller());
}
private ServiceInstaller GetServiceInstaller()
{
serviceInstaller.ServiceName = GetConfigurationValue("ServiceName");
serviceInstaller.Description = GetConfigurationValue("Description");
serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
return serviceInstaller;
}
private ServiceProcessInstaller GetServiceProcessInstaller()
{
serviceProcessinstaller.Account = ServiceAccount.LocalSystem;
return serviceProcessinstaller;
}
Run Code Online (Sandbox Code Playgroud)
如何创建事件日志?
获取 Event1 后,我需要获取在 Event1之后一段未知时间发生的另一个事件的第一个实例。然后获取在 Event2 之后某个时间发生的 Event3 等。
基本上从以下开始:
$filterXML = @'
<QueryList>
<Query Id="0" Path="System">
<Select Path="System">*[System[Provider[@Name='Microsoft-Windows-Kernel-General'] and (Level=4 or Level=0) and (EventID=12)]]</Select>
</Query>
</QueryList>
'@
$event1=(Get-WinEvent -ComputerName $PCname -MaxEvents 1 -FilterXml $filterXML).timecreated
Run Code Online (Sandbox Code Playgroud)
给我 Event1 的日期时间。然后我想做类似的事情:
Get-WinEvent -LogName "System" -MaxEvents 1 -FilterXPath "*[EventData[Data = 'Windows Management Instrumentation' and TimeCreated -gt $event1 ]]"
显然,时间创建的粗体部分不起作用,但我希望你明白我想要做的事情。有什么帮助吗?
我想也许filterhashtable就是我需要走的路?寻求澄清:
$Event2=(Get-WinEvent -Oldest -MaxEvents 1 -FilterHashtable @{logname="system"; providername="Microsoft-Windows-GroupPolicy"; starttime=$Event1}).TimeCreated
Run Code Online (Sandbox Code Playgroud) 我有以下代码用于记录我通过在线研究找到的Windows服务.它在我的服务类内部完成之前就像在下面初始化一样.
public GBBInvService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MyLogSource"))
System.Diagnostics.EventLog.CreateEventSource("MyLogSource",
"MyDoLog");
eventLog1.Source = "MyLogSource";
eventLog1.Log = "MyDoLog";
}
Run Code Online (Sandbox Code Playgroud)
记录时的代码:
eventLog1.WriteEntry("GBBInvService Service Started");
Run Code Online (Sandbox Code Playgroud)
然而,我的顾问却反对我.他的评论如下:
我会避免让服务在运行时创建事件源.这要求服务以高权限运行(即,比实际需要更多地访问计算机).设置Windows事件日志源实际上是一个安装时间作业.您可以将事件日志安装程序添加到项目安装程序文件(以及服务安装程序),然后事件源将始终存在
这个建议的问题是我无法找到在项目安装程序文件中创建日志的任何示例.我还尝试将此日志创建部分移动到我的项目安装程序中,但之后它不会让我从我的Web服务cs页面调用或写入eventlog1.他还建议使用log4net,但这对我来说是一个新的东西,并且很难掌握.刚刚完成我的第一个Windows服务项目,我对Windows服务仍然很陌生,并且非常感谢在项目安装程序中创建日志的任何方向,从我的服务cs页面或在log4net上的任何抬头写入它.
我需要将 Windows 事件日志保存在某个文件中,现在我正在使用:
var els = new EventLogSession();
els.ExportLogAndMessages("Application", PathType.LogName, "*", Path.Combine("c:\\Application.evtx"), false, CultureInfo.CurrentCulture);
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我想获取DateTime范围之间的日志,我怀疑ExportLogAndMessages“查询”中的第三个参数可能对我有帮助。
现在如何编写这个“查询”,如果“查询”没有帮助,无论如何都可以这样做。
我有一个使用 EventLog 进行日志记录的 Windows 服务应用程序。在应用程序安装程序中我运行:
eventcreate /L APPLICATION /SO "My App" /T SUCCESS /id 1 /D "Initialised Log"
Run Code Online (Sandbox Code Playgroud)
然后在我的 C# 应用程序记录器中执行以下操作:
EventLog.WriteEntry(message, EventLogEntryType.Error, 1, 0, details);
Run Code Online (Sandbox Code Playgroud)
但是,当我查看应用程序事件日志时,除了我的事件之外,我还看到 EventID 0 的条目。我无法使用 eventcreate 创建 ID=0 条目(表示 ID 必须 >=1)。那么是什么造成了这些事件呢?有什么方法可以阻止事件日志抱怨安装损坏吗?
一个示例条目说:
该活动包含以下信息:
服务启动成功。
消息资源存在,但在字符串/消息表中找不到该消息
使用该EvtExportLog函数,我目前无法为Pathand / or Query参数指定正确的值。
我的目标是导出本地应用程序和系统事件日志。
我试过了:
EvtExportLog(
IntPtr.Zero,
"Application",
"*",
"C:\\SomePath\\Application.evtx",
EventExportLogFlags.LogFilePath);
Run Code Online (Sandbox Code Playgroud)
具有以下P / Invoke定义:
[Flags]
private enum EventExportLogFlags
{
ChannelPath = 1,
LogFilePath = 2,
TolerateQueryErrors = 0x1000
};
[DllImport(@"wevtapi.dll",
CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Auto,
SetLastError = true)]
private static extern bool EvtExportLog(
IntPtr sessionHandle,
string path,
string query,
string targetPath,
[MarshalAs(UnmanagedType.I4)] EventExportLogFlags flags);
Run Code Online (Sandbox Code Playgroud)
不幸的是,该函数返回false并且最后一个错误代码为2(ERROR_FILE_NOT_FOUND)。
我的问题:
要在Path和Query参数中输入什么以导出本地应用程序和系统事件日志?
我正在使用 Windows 事件日志来记录来自自定义 Windows 服务的日志。
如果我需要阅读昨天的日志,我会转到 EventViewer,查找我的 EventLog 并阅读它。但是我担心如果我需要访问几个月甚至几年前的日志会发生什么。是否可以?Windows 将我的日志存储多长时间?它可以从我的服务中配置吗?
我是否应该使用像 NLog 这样的第三方记录器,它可能比我的服务的 Windows 事件日志更好?