简单的NCron服务:

class Program
{
    static void Main(string[] args)
    {
        var schedService = new SchedulingService();
        schedService.At("* * * * *").Run<MyTask>(); // run every minute
        schedService.Start();

        Console.ReadLine();
    }
}

public class MyTask : NCron.ICronJob
{
    public void Execute()
    {
        Console.WriteLine("executing");
    }

    public void Initialize(NCron.CronContext context)
    {
    }

    public void Dispose()
    {
    }
}


到达第一分钟但执行MyTask之前,NCron似乎要写入Windows事件日志,并失败

Unhandled Exception: System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  To create the source, you need permission to read all event logs to make sure that the new source name is unique.  Inaccessible logs: Security.
   at System.Diagnostics.EventLogInternal.FindSourceRegistration(String source, String machineName, Boolean readOnly, Boolean wantToCreate)
   at System.Diagnostics.EventLogInternal.SourceExists(String source, String machineName, Boolean wantToCreate)
   at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)
   at NCron.ExceptionHelper.LogUnhandledException(Object exception)
   at NCron.Service.SchedulingService.WaitCallbackHandler(Object data)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()`


NCron将什么以及为什么写入事件日志?这是默认行为,因此,我想使用NCron的任何人都必须对此进行处理,但是我找不到有关此问题的任何文档或问题/解答。我尝试设置schedService.LogFactory = null;,它不会改变任何内容。

缺少创建自定义日志工厂(我不想这样做)或摆弄注册表(我真的不想这样做,有时在生产计算机上有时无法做到)的方法,该如何解决呢?

最佳答案

如果您不想写入事件日志,则需要提供备用的记录器和工厂。如https://code.google.com/p/ncron/wiki/Logging页底部所述,有一个log4net实现可用,其最新版本似乎位于https://github.com/schourode/ncron/tree/master/src/NCron.Integration.log4net。如果您不想使用log4net,则编写备用实现将非常简单。

如果您愿意允许NCron写入事件日志,则可以在运行服务之前通过在管理员帐户下创建目标事件日志来避免运行时SecurityException。通常,这可以通过在安装程序中包括创建事件日志来完成(假设您有一个),例如使用EventLogInstaller

关于c# - NCron默认记录器写入事件日志,引发SecurityException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14649961/

10-13 07:09