简体   繁体   English

获取在C#中的WiX安装程序中设置的事件源的名称

[英]Get name of Event Source set up in WiX installer, in C#

I need to do some custom writing to the Event Log from a C# application. 我需要从C#应用程序对事件日志进行一些自定义写入。 Because I won't have administrator permissions at run-time, I'm creating the event source in the WiX installer, as so: 因为我在运行时没有管理员权限,所以要在WiX安装程序中创建事件源,如下所示:

<u:EventSource Name="SourceName" Log="Application" EventMessageFile="[NETFRAMEWORK40FULLINSTALLROOTDIR]EventLogMessages.dll" />

and then writing by: 然后写:

 EventLog.WriteEntry("SourceName", message, EventLogEntryType.Error);

The issue here, is that SourceName must be kept in sync between my installer, and C# codebase - it previously has got out of sync. 这里的问题是SourceName必须在我的安装程序和C#代码库之间保持同步-以前它已经不同步。

I'm currently using the main file's name to link the two, as below: 我目前正在使用主文件的名称来链接两者,如下所示:

<?define MainExe="MyMain.exe" ?>
<u:EventSource Name="$(var.MainExe)" Log="Application" EventMessageFile="[NETFRAMEWORK40FULLINSTALLROOTDIR]EventLogMessages.dll" />

and: 和:

var name = Application.ResourceAssembly.ManifestModule.Name;
EventLog.WriteEntry(name, message, EventLogEntryType.Error);

But this feels very hacky. 但这感觉很hacky。 Is there a proper to dynamically get the name of an event source set up in a WiX installer, or do I just have to bodge the best I can? 是否有合适的方法可以动态获取在WiX安装程序中设置的事件源的名称,还是我必须尽力而为?

I think you are taking DRY a little too far. 我认为您将DRY调得太远了。 Maintain it in two places. 在两个地方进行维护。 If you really can't do that the WiX installer could transform the app.config and save in a setting what the name of the event source is. 如果您真的做不到,那么WiX安装程序可以转换app.config并将其保存在设置中事件源的名称。 But really, it's OK to have 1 constant in the .wxs and 1 in the .cs. 但实际上,在.wxs中具有1个常量,在.cs中具有1个常量是可以的。

The only time I've ever done the config file is when I was doing product line and different consumers of a framework would be distinct branded applications with their own unique logs. 我做过配置文件的唯一一次是在做产品线时,框架的不同使用者将是拥有自己独特日志的独特品牌应用程序。

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

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