Log4Net doesn't log custom exception correct -
i'm using log4net , logging exceptions. want log current object on exception time couldn't. created exception , add together object exception's info property log exception log4net. log4net's exceptions message doesn't contain object.
my code such as;
seek { exception exp = new exception("critical error"); exp.data.add("eror object", viewmodel); throw exp; } grab (exception exp) { _logmanager.error(exp); }
my viewmodel object;
[serializable] public class carttransferviewmodel { public carttransferviewmodel() { model = new modelobject(); } public modelobject model { get; set; } public string informatinmessage { get; set; } public bool? isactive { get; set; } }
and model object serializable too. log4net's exception message this;
system.exception: critical error @ fooproject.fooclass.foomethod() in d:\fooproject\fooclass.cs:line 200
i'm remove serializable attribute re run application, error code did alter to;
system.argumentexception: argument passed in not serializable. parameter name: value @ system.collections.listdictionaryinternal.add(object key, object value) @ fooproject.fooclass.foomethod() in d:\fooproject\fooclass.cs:line 200
how log custom exception objects?
ok ,if got question. similar . utilize logger write exception . utilize exception.data property.
here illustration , illustration contains 1) info class , needs written 2) sample class method , write infoclass when exception happens 3) utility class formats exception
[serializable] public class flatfileitem { arraylist errorlist = new arraylist(); public flatfileitem() { if (errorlist == null) { errorlist = new arraylist(); } } //name of file public string filename { get; set; } public override string tostring() { homecoming string.format(@"flatfileitem (unzip ftplineitem) => filename:{0}", this.filename); } } public class someclass { public void somemethod(){ try{ // throw exception here } grab (exception ex) { ex.data["flatfile"] = convert.tostring(flatfile); //using info property flatfile.haserrors = true; //not there in above illustration flatfile.parent.adderrorinfo(ex); //not there in above illustration logger.error(string.format(ex.message)); //not there in above illustration throw ( new exception ("yourmsg",ex)); //if want } } }
//now utilize utility method write out @ top level exception
public class exceptioninfoutil { public static string getallexceptioninfo(exception ex) { stringbuilder sbexception = new stringbuilder(); int = 1; sbexception.append(getexceptioninfo(ex, i)); while (ex.innerexception != null) { i++; ex = ex.innerexception; sbexception.append(getexceptioninfo(ex, i)); } homecoming sbexception.tostring(); } private static string getexceptioninfo(exception ex, int count) { stringbuilder sbexception = new stringbuilder(); sbexception.appendline(string.format("")); sbexception.appendline(string.format("")); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format(" inner exception : no.{0} ", count)); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format("==================================================")); sbexception.appendline(string.format(" error message : {0} ", ex.message)); sbexception.appendline(string.format("==================================================")); #region mine thru info dictionary seek { sbexception.appendline(string.format("==================================================")); sbexception.appendline(string.format(" info parameters count @ source :{0}", ex.data.count)); sbexception.appendline(string.format("==================================================")); string skey = string.empty; foreach (object key in ex.data.keys) { seek { if (key != null) { skey = convert.tostring(key); sbexception.appendline(string.format(" key :{0} , value:{1}", skey, convert.tostring(ex.data[key]))); } else { sbexception.appendline(string.format(" key null")); } } grab (exception e1) { sbexception.appendline(string.format("** exception occurred when writting log *** [{0}] ", e1.message)); } } } grab (exception ex1) { sbexception.appendline(string.format("** exception occurred when writting log *** [{0}] ", ex1.message)); } #endregion sbexception.appendline(string.format("==================================================")); sbexception.appendline(string.format(" source : {0} ", ex.source)); sbexception.appendline(string.format("==================================================")); sbexception.appendline(string.format(" stacktrace : {0} ", ex.stacktrace)); sbexception.appendline(string.format("==================================================")); sbexception.appendline(string.format(" targetsite : {0} ", ex.targetsite)); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format(" finished writting exception info :{0} ", count)); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format("************************************************")); sbexception.appendline(string.format("")); sbexception.appendline(string.format("")); homecoming sbexception.tostring(); } }
log4net
No comments:
Post a Comment