不幸的是,事件源需要创建管理权限。但是,您不需要管理员权限即可写入事件日志或从中读取。
有两种方法可以解决这个问题。
在以管理员身份安装应用程序时添加新的事件源。
您创建一个以管理员身份运行的简单应用程序来配置您的应用程序。这甚至可以包含在安装程序中。
如果您没有或不想要安装程序,请以管理员身份将该应用程序加载到计算机上并运行该程序一次。如果事件源不存在,您的应用启动应该配置事件源,对吗?(好吧,这是三种方式。)
此代码片段来自 microsoft: http: //msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx,旨在以管理员用户身份运行。
using System;
using System.Diagnostics;
using System.Threading;
class MySample
{
public static void Main()
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
}
我知道这可能不是你所追求的,但我认为这是做到这一点的唯一方法。
编辑 1:添加了更多代码
public class EventLogger
{
private const string logName = "Application";
private static string appName = "";
private static bool sourceExists = false;
public static string AppName
{
get { return appName; }
set { appName = value; }
}
public static void Init(string appName)
{
AppName = appName;
sourceExists = EventLog.SourceExists(AppName);
if (!sourceExists)
{
EventLog.CreateEventSource(AppName, logName);
sourceExists = true;
}
}
private static void Write(string entry, EventLogEntryType logType, int eventID)
{
if (sourceExists)
{
EventLog.WriteEntry(AppName, entry, logType, eventID);
}
}
public static void Warning(string entry) { Write(entry, EventLogEntryType.Warning, 200); }
public static void Warning(string entry, int eventID) { Write(entry, EventLogEntryType.Warning, eventID); }
public static void Error(string entry) { Write(entry, EventLogEntryType.Error, 300); }
public static void Error(string entry, int eventID) { Write(entry, EventLogEntryType.Error, eventID); }
public static void Info(string entry) { Write(entry, EventLogEntryType.Information, 100); }
public static void Info(string entry, int eventID) { Write(entry, EventLogEntryType.Information, eventID); }
}
这是我实现在生产应用程序中使用的 EventLogger 类的方式。
如果您可以发布您的代码,我们可以进行比较。
我想到的一件事是,当我创建源时,我使用应用程序名称,但坚持使用应用程序日志文件。您是否还尝试创建新的日志文件。如果是这样,请检查它是否在事件查看器中创建。
编辑 2:使用用户令牌值为零模拟用户
这有点麻烦。
试试这个代码,包裹着事件编写代码。
System.Security.Principal.WindowsImpersonationContext wic = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero);
// your code to write to event log or any to do something which needs elevated permission--
wic.Undo();
我没有尝试过,只是因为我的代码正在运行,它来自这里:http ://sharenotes.wordpress.com/2008/03/18/cannot-open-log-for-source-you-may-not -具有写访问权限/