在CurrentDomain_UnhandledException处未处理System.TypeInitializationException HResult = -2146233036 ...

用户名

目前,我正在尝试使用隔离存储将错误日志写入磁盘文件。突然,我开始收到以下异常:

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);
        }
    }

请注意,在我开始使用隔离存储之前,仅在与可执行文件位于同一目录中写入文件时,它可以正常工作。我已经花了一些时间在互联网上搜索,但是没有找到与我收到的异常相同且内部异常相同的任何帖子。

是否有人对我为什么收到上述错误有任何想法?TIA。

敦促

您似乎假设您的静态属性按固定顺序进行了初始化。

如果需要一个序列,请编写一个静态构造函数,并按照需要对其进行初始化的序列对其进行初始化。

例如,谁说_assembly这行已经初始化了:

private static string _errorLogFile = _assembly.FullName + ".log";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章