2013-02-01 129 views
2

简单NCron服务:NCron默认记录写入到事件日志,抛出抛出:SecurityException

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;,这不会改变任何东西。

短创建一个自定义日志工厂(我不想做),或用注册表摆弄(我真的不想做,有时不能生产机器的), 我怎样才能解决这个问题?

回答

1

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

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

+0

我发现令人失望和不幸的是,NCron开箱即不能正常工作。我会检查我的选项,谢谢。 – epalm

+0

如果您对其默认行为不满意,则可以随时通过https://code.google.com/p/ncron/issues/list提交更改请求。 –

+0

如果您的应用程序是围绕NCron的'Bootstrap.Init()'方法构建的,则会提供事件日志安装程序。只需用'install'作为唯一的命令行参数运行你的EXE文件,它就会将应用程序设置为Windows服务并创建事件源。 –

相关问题