2014-05-15 2 views
0

현재 격리 된 저장소를 사용하여 디스크 로그 파일에 오류 로그를 쓰려고합니다.System.TypeInitializationException이 처리되지 않았습니다. HResult = -2146233036 ... CurrentDomain_UnhandledException에서

다음
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.

+1

이 오류는 일반적으로 발생합니다. 가능한 오류에 대해서는 생성자를 확인해야합니다. –

답변

1

정적 속성이 고정 된 순서로 초기화된다고 가정하는 것처럼 보입니다.

시퀀스가 ​​필요한 경우 정적 생성자를 작성하고 초기화해야하는 시퀀스로 초기화합니다. 예를 들어

는 누가 _assembly 이미이 줄 초기화 말한다 : 정적 생성자에 오류가 어딘가에있을 때

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

고마워요. 디버그 모드로 테스트했을 때 문제없이 작동했지만 "라이브"상황에서 실행하기 위해 응용 프로그램을 설치 한 후에는 이니셜 라이저가 다른 순서로 실행되어 오류가 발생해야합니다. – Roger

관련 문제