![](/img/trans.png)
[英]How to create custom properties keypress event in propertygrid using vb.net or c#
[英]How to create a custom event log using C#
我创建了一个 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";
}
}
在初始化期间,安装了服务并且不创建日志。 日志条目位于系统的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;
}
如何创建事件日志?
将您的代码更改为以下内容:
if (!System.Diagnostics.EventLog.SourceExists(source: sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(source: sourceName, logName: logName);
}
请注意, 根据 Microsoft 的 KB ,事件日志名称的前 8 个字符必须与计算机上的所有其他事件日志不同(因此,如果用户的计算机已有名为"Application"
的日志,则您不能创建名为"Applicat1"
的新事件EventLog
"Applicat1"
或"ApplicationFoobar"
因为它们与内置Application
事件日志共享相同的 8 个字符)。
ServiceName 和 Source 必须是不同的名称。
服务名称
this.serviceInstaller1.ServiceName = "MaliyeWMService";
来源
if (!System.Diagnostics.EventLog.SourceExists("MaliyeMailService"))
{
System.Diagnostics.EventLog.CreateEventSource("MaliyeMailService", "MaliyeMailServiceLog");
}
OlayLog.Source = "MaliyeMailService";
首先尝试将 AutoLog 设置为 false。
this.AutoLog = false;
if (!System.Diagnostics.EventLog.SourceExists(sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
}
eventLog.Source = "MySource";
在这个 MSDN 演练中,他们似乎完全省略了这一步。 但是,在此 MSDN“ 如何: ”中,他们声明:
如果要写入应用程序日志以外的事件日志,则必须将 AutoLog 属性设置为 false,在服务代码中创建您自己的自定义事件日志,并将您的服务注册为该日志的有效条目源。
将 AutoLog 设置为 false 后,我的自定义日志和事件按预期显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.