0

現在、分離ストレージを使用してディスク ファイルにエラー ログを書き込もうとしています。突然、次の例外を受け取り始めました。

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'MyAppUtilities.ErrorLogger' threw an exception.
  Source=MyAppUtilities
  TypeName=MyAppUtilities.ErrorLogger
  StackTrace:
       at MyAppUtilities.ErrorLogger.AuditMethodError(Exception exc, String threadName,    String service)
       at MyWatchdog.Program.CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e) in c:\DataService\MyWatchdog\Program.cs:line 20
  InnerException: System.ArgumentNullException
      HResult=-2147467261
      Message=Value cannot be null.
  Parameter name: path
      Source=mscorlib
      ParamName=path
      StackTrace:
        at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
        at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
        at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, IsolatedStorageFile isf)
        at MyAppUtilities.ErrorLogger..cctor() in c:\DataService\DataServiceUtilities\ErrorLogger.cs:line 57
   InnerException:  

基本的に、エラーメッセージを書くために使用しているコードは次のとおりです。

    private static Assembly _assembly =
        System.Reflection.Assembly.GetExecutingAssembly();
    private static string _errorLogFile =
        _assembly.FullName + ".log";
    private static IsolatedStorageFile _isoStore =
        IsolatedStorageFile.GetStore(
        IsolatedStorageScope.User |
        IsolatedStorageScope.Assembly,
        null,
        null);
    private static IsolatedStorageFileStream _isoStream =
        new IsolatedStorageFileStream(_errorLogFile,
        FileMode.OpenOrCreate, _isoStore);

    public static void ApplicationAudit(string message)
    {
        using (StreamWriter sw = new StreamWriter(_isoStream))
        {
            _isoStream.Seek(0, SeekOrigin.End);
            sw.WriteLine("--------------------------------------------" +
                "------------------------------");
            sw.WriteLine(DateTime.Now.ToString() + ": " + message);
        }
    }

分離ストレージの使用を開始する前は、実行可能ファイルと同じディレクトリにあるファイルに書き込むだけで問題なく機能していたことに注意してください。私はインターネットを検索するのに時間を費やしましたが、受け取ったものと同じ内部例外と同じ例外を持つ投稿は見つかりませんでした。

上記のエラーが表示される理由について誰か考えがありますか? ティア。

4

1 に答える 1

1

静的プロパティが固定された順序で初期化されると想定しているようです。

シーケンスが必要な場合は、静的コンストラクターを作成し、必要な順序で初期化します。

たとえば、_assembly次の行で既に初期化されていると言う人:

private static string _errorLogFile = _assembly.FullName + ".log";
于 2014-05-15T15:28:39.780 に答える