c# - What is and how to fix System.TypeInitializationException error? -
private static void main(string[] args) { string str = null; logger.inituserlogwithrotation(); // <--- error occur ... }
when build project, has no error. when execute it, aborted.
i tried debug project , system.typeinitializationexception error occurred @ first line.
i've tried googling , yet found no solution.
it seems variable initialize code wrong , can't find it.
please me. i'm new c#.
thanks.
※ here logger class code
public class logger { private static int hdlog_priority_debug = 4; private static int hdlog_priority_error = 1; private static int hdlog_priority_fatal = 0; private static int hdlog_priority_info = 3; private static int hdlog_priority_warning = 2; public static int log_level_debug = 4; public static int log_level_error = 2; public static int log_level_fatal = 1; public static int log_level_info = 5; public static int log_level_warning = 3; private static string s_bstcommonappdata = path.combine(s_commonappdata, "xxxx"); private static string s_bstuserdatadir = path.combine(s_bstcommonappdata, "userdata"); private static string s_commonappdata = environment.getfolderpath(environment.specialfolder.commonapplicationdata); private static bool s_consolelogging = false; private static filestream s_filestream; public static hdloggercallback s_hdloggercallback; private static string s_logdir = null; private static string s_logfilename = "xxxx"; private static string s_logfilepath = null; public static int s_logfilesize = 0xa00000; private static bool s_loggerinited = false; private static string s_loglevels = null; private static int s_logrotationtime = 0x7530; private static string s_logstringdebug = "debug"; private static string s_logstringerror = "error"; private static string s_logstringfatal = "fatal"; private static string s_logstringinfo = "info"; private static string s_logstringwarning = "warning"; private static int s_processid = -1; private static string s_processname = "unknown"; private static object s_sync = new object(); public static int s_totallogfilenum = 5; private static textwriter writer = console.error; private static void close() { if (!s_consolelogging) { writer.close(); s_filestream.dispose(); writer.dispose(); } } public static void debug(string msg) { debug("{0}", new object[] { msg }); } public static void debug(string fmt, params object[] args) { print(log_level_debug, s_processname, fmt, args); } private static void dologrotation() { label_0000: thread.sleep(s_logrotationtime); try { lock (s_sync) { fileinfo info = new fileinfo(s_logfilepath); if (info.length >= s_logfilesize) { string destfilename = s_logfilepath + ".1"; string path = s_logfilepath + "." + s_totallogfilenum; if (file.exists(path)) { file.delete(path); } (int = s_totallogfilenum - 1; >= 1; i--) { string str3 = s_logfilepath + "." + i; string str4 = s_logfilepath + "." + (i + 1); if (file.exists(str3)) { file.move(str3, str4); } } file.move(s_logfilepath, destfilename); } } goto label_0000; } catch (exception) { goto label_0000; } } public static void error(string msg) { error("{0}", new object[] { msg }); } public static void error(string fmt, params object[] args) { print(log_level_error, s_processname, fmt, args); } public static void fatal(string msg) { fatal("{0}", new object[] { msg }); } public static void fatal(string fmt, params object[] args) { print(log_level_fatal, s_processname, fmt, args); } private static string getlogdir(bool userspecificlog) { string str; if (s_logdir != null) { return s_logdir; } try { if (userspecificlog) { str = path.combine(s_bstuserdatadir, "logs"); } else { str = (string) registry.localmachine.opensubkey(@"software\xxxx").getvalue("logdir"); } } catch (exception) { str = path.combine(s_bstuserdatadir, "logs"); } s_logdir = str; return str; } private static string getprefix(string tag, string loglevel) { int managedthreadid = thread.currentthread.managedthreadid; datetime = datetime.now; return string.format("{0:d4}-{1:d2}-{2:d2} {3:d2}:{4:d2}:{5:d2}.{6:d3} {7}:{8:x8} ({9}). {10}: ", new object[] { now.year, now.month, now.day, now.hour, now.minute, now.second, now.millisecond, s_processid, managedthreadid, tag, loglevel }); } public static textwriter getwriter() { return new writer(delegate (string msg) { print(msg); }); } private static void hdlogger(int prio, uint tid, string tag, string msg) { int level = 0; if (prio == hdlog_priority_fatal) { level = log_level_fatal; } else if (prio == hdlog_priority_error) { level = log_level_error; } else if (prio == hdlog_priority_warning) { level = log_level_warning; } else if (prio == hdlog_priority_info) { level = log_level_info; } else if (prio == hdlog_priority_debug) { level = log_level_debug; } print(level, tag, "{0:x8}: {1}", new object[] { tid, msg }); } public static void info(string msg) { info("{0}", new object[] { msg }); } public static void info(string fmt, params object[] args) { print(log_level_info, s_processname, fmt, args); } public static void initconsolelog() { initlog("-", true, false); } public static void initlog(string logfilename, bool userspecificlog, bool dologrotation) { s_loggerinited = true; s_hdloggercallback = new hdloggercallback(logger.hdlogger); s_processid = process.getcurrentprocess().id; s_processname = process.getcurrentprocess().processname; if (logfilename == "-") { writer = console.error; s_consolelogging = true; } else { if (logfilename == null) { logfilename = s_logfilename; } if (userspecificlog) { logfilename = logfilename + "users"; } string logdir = getlogdir(userspecificlog); string str2 = string.format(@"{0}\{1}.log", logdir, logfilename); if (!directory.exists(logdir)) { directory.createdirectory(logdir); } s_logfilepath = str2; loglevelsinit(); lock (s_sync) { open(); } if (dologrotation) { new thread(() => dologrotation()) { isbackground = true }.start(); } } } public static void initsystemlog() { initlog(null, false, false); } public static void initsystemlogwithrotation() { initlog(null, false, true); } public static void inituserlog() { initlog(null, true, false); } public static void inituserlogwithrotation() { initlog(null, true, true); } private static bool isloglevelenabled(string tag, string level) { if (s_loglevels == null) { return false; } return (s_loglevels.startswith("all") || s_loglevels.contains((tag + ":" + level).toupper())); } private static void loglevelsinit() { string name = @"software\xxxx\config"; try { using (registrykey key = registry.localmachine.opensubkey(name)) { s_loglevels = (string) key.getvalue("debuglogs"); } } catch (exception) { return; } if (s_loglevels != null) { s_loglevels = s_loglevels.toupper(); } } private static void open() { if (!s_consolelogging) { if (!s_loggerinited) { initlog("-", false, false); s_loggerinited = true; } else { s_filestream = new filestream(s_logfilepath, filemode.append, fileaccess.write, fileshare.delete | fileshare.readwrite); writer = new streamwriter(s_filestream, encoding.utf8); } } } public static void print(string msg) { print("{0}", new object[] { msg }); } public static void print(string fmt, params object[] args) { print(log_level_info, s_processname, fmt, args); } public static void print(int level, string tag, string fmt, params object[] args) { string str = "unknown"; if (level == log_level_fatal) { str = s_logstringfatal; } else if (level == log_level_error) { str = s_logstringerror; } else if (level == log_level_warning) { str = s_logstringwarning; } else if (level == log_level_info) { str = s_logstringinfo; } else if (level == log_level_debug) { str = s_logstringdebug; } if ((level != log_level_debug) || isloglevelenabled(tag, str)) { lock (s_sync) { open(); writer.writeline(getprefix(tag, str) + fmt, args); writer.flush(); close(); } } } public static void setlogdir(string logdir) { s_logdir = logdir; } public static void warning(string msg) { warning("{0}", new object[] { msg }); } public static void warning(string fmt, params object[] args) { print(log_level_warning, s_processname, fmt, args); } public delegate void hdloggercallback(int prio, uint tid, string tag, string msg); public class writer : textwriter { private writefunc writefunc; public writer(writefunc writefunc) { this.writefunc = writefunc; } public override void writeline(string msg) { this.writefunc(msg); } public override void writeline(string fmt, object obj) { this.writefunc(string.format(fmt, obj)); } public override void writeline(string fmt, object[] objs) { this.writefunc(string.format(fmt, objs)); } public override system.text.encoding encoding { { return system.text.encoding.utf8; } } public delegate void writefunc(string msg); } }
whenever typeinitializationexception
thrown, check initialization logic of type referring first time in statement exception thrown - in case: logger
.
initialization logic includes: type's static constructor (which - if didn't miss - not have logger
) , field initialization.
field initialization pretty "uncritical" in logger
except following lines:
private static string s_bstcommonappdata = path.combine(s_commonappdata, "xxxx"); private static string s_bstuserdatadir = path.combine(s_bstcommonappdata, "userdata"); private static string s_commonappdata = environment.getfolderpath(environment.specialfolder.commonapplicationdata);
s_commonappdata
null
@ point path.combine(s_commonappdata, "xxxx");
called. far i'm concerned, these initializations happen in exact order wrote them - put s_commonappdata
@ least 2 lines ;)
Comments
Post a Comment