2

セマンティック ログ アプリケーション ブロックによって (RollingFlatFileLogs を介して) 消費される ETW EventSource の非常に単純な実装を作成しました。

基本的なセットアップは次のとおりです。

[EventSource(Name = "My Applicaiton")]
public partial class MyEventSource : EventSource
{
    /// <summary>
    /// The log
    /// </summary>
    public static MyEventSource Log = new MyEventSource();

...

これには、基本的なキーワード、タスク、およびオペコードがあります。

ファイルは、次のようなイベントによって書き込まれます。

/// <summary>
    /// Application starting.
    /// </summary>
    [Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.AppStart, Opcode = Opcodes.Start, Message = "Application Starting")]
    public void ApplicationStarting()
    {
        if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
        {
            this.WriteEvent(100);
        }
    }

この方法を使用してサブスクライブします。

var listener = Microsoft.Practices.EnterpriseLibrary.SemanticLogging.RollingFlatFileLog.CreateListener("C:\\ServiceRegistry\\services.log", 5120, null, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollFileExistsBehavior.Increment, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollInterval.None, null, 10, true);

        listener.EnableEvents(MyEventSource.Log, System.Diagnostics.Tracing.EventLevel.LogAlways);


        // Log some things
        MyEventSource.Log.ApplicationStarting();

ただし、私たちが確認した問題は、一部の開発者が実行を停止するログ ファイルをロックしていることです。「ファイルは使用中のため、書き込みができません」というメッセージが表示されます。

なぜこうなった?ロギングが非同期で実行されているにもかかわらず、プロセスが終了するのはなぜですか?

今後、このようなことが起こらないようにするにはどうすればよいでしょうか。このようなエラーが発生した場合にファイルをロールできるように、ロギングの実装を強化する方法はありますか?

ありがとう!

編集:問題のスタック トレースをキャプチャしました:

 The process cannot access the file 'C:\ServiceRegistry\services.log' because it is               being used by another process.

    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
     at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
    at Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollingFlatFileSink..ctor(String fileName, Int32 rollSizeKB, String timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, Int32 maxArchivedFiles, Boolean isAsync)
    at Microsoft.Practices.EnterpriseLibrary.SemanticLogging.RollingFlatFileLog.CreateListener(String fileName, Int32 rollSizeKB, String timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, IEventTextFormatter formatter, Int32 maxArchivedFiles, Boolean isAsync)
4

1 に答える 1