Reputation: 7686
Presently, I am attempting to write error logs to a disk file using isolated storage. Suddenly, I started receiving the following exception:
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:
Here is basically the code I am using to write error messages:
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);
}
}
Please note that, before I started using isolated storage, it worked fine when just writing to a file in the same directory as the executable. I have spent some time searching the internet, but haven't found any posts with the same exception together with the inner exception as the one I am receiving.
Does anyone have any ideas as to why I am receiving the above error? TIA.
Upvotes: 0
Views: 4646
Reputation: 77334
You seem to assume that your static properties get initialized in a fixed sequence.
If you need a sequence, write a static constructor and initialize them in the sequence you need them initialized.
For example, who says that _assembly
is already initialized in this line:
private static string _errorLogFile = _assembly.FullName + ".log";
Upvotes: 1