AutoLog property to . This will prevent events from being written by default to the Windows Application log .
If you have not already done so, right-click anywhere in the [Design] view of the service component, and select the ServiceInstaller
Add Installer
menu option. This adds a component to your solution namedby default.
Note that the new ServiceBase.EventLog.Log
is automatically opened to its respective [Design] view. To get to its code, right-click the ProjectInstaller.cs file in the Solution Explorer, and select the View Code menu option.EventLog.Log
Change the contents of that file to look like the following. This updates the
to use the custom log name of your choice.
The last step is to tie the service component to the custom event log you named in Step 6. To do that, right-click the service component in the Solution Explorer, and select the View Code menu option. Update the constructor to the following:ServiceInstaller
When you install the service, the
YourEventLogName
should be created on the system. If the Event Viewer is already open, you'll probably have to refresh it. If it's still not visible, you might need to log something to it (I don't recall the details). In any case, to write log information to the custom event log from your service, use the我目前正在创建一个Windows服务,刚刚发现(感谢 这个 答)如何配置服务安装程序,以便在安装过程中创建一个自定义事件日志源。我自己已经弄清楚了,这些自定义事件日志源需要高权限才能注册。这就是为什么在安装过程中发生注册的原因--因为服务安装总是以高权限执行。到目前为止,还算不错。
ServiceBase
:false
? 我应该在哪里做呢?ProjectInstaller
我希望得到关于这两种方法的可行性的答案。ProjectInstaller
EventLogInstaller
更多详情 using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
namespace YourProjectNamespace
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Log = "YourEventLogName"; // enter your event log name here
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
{
return eventLogInstaller;
}
}
return null;
}
}
}
public YourServiceName()
{
InitializeComponent();
// This ties the EventLog member of the ServiceBase base class to the
// YourEventLogName event log created when the service was installed.
EventLog.Log = "YourEventLogName";
}
EventLog
打开[设计]视图后,按F4(或View