2

シンプルな 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()
    {
    }
}

最初の 1 分間に達した後、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;してみましたが、何も変わりません。

カスタム ログ ファクトリを作成したり (これはやりたくない)、レジストリをいじったり (これは本当にやりたくないし、実稼働マシンではできないこともあります) を除いて、どうすればこれを修正できますか?

4

1 に答える 1

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を使用して、インストーラーにイベント ログの作成を含めることによって行われます (あると仮定します) 。

于 2013-02-01T17:10:27.863 に答える