Poo*_*oja 4 .net c# windows-services event-log
我创建了一个 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)
如何创建事件日志?
将您的代码更改为以下内容:
if (!System.Diagnostics.EventLog.SourceExists(source: sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(source: sourceName, logName: logName);
}
Run Code Online (Sandbox Code Playgroud)
请注意,根据 Microsoft 的 KB,事件日志名称的前 8 个字符必须与计算机上的所有其他事件日志不同(因此,如果用户的计算机已有一个名为的日志,"Application"则您无法创建新的EventLog命名日志"Applicat1",或者"ApplicationFoobar"因为它们共享该日志)与内置Application事件日志相同的 8 个字符)。
| 归档时间: |
|
| 查看次数: |
26757 次 |
| 最近记录: |