How to mark the current method in a log file?
I'm writing an exception logger class. My question is: what to use to mark the current method? Except it's name, cause the code will be obfuscated, so it can开发者_C百科't be used.
You can invent your own attribute and decorate your methods with the attribute.
Something like [MethodName("WriteXMLData")]
You can then have the logger class perform some reflection on the MemberInfo object passed to it during logging.
This is a great tutorial for defining and querying your own attributes.
You might want to take a look at some of the logging frameworks out there. I'm partial to NLog. It's easy to configure and has a lot of flexibility.
you can get all the data u need about the specific exception, and log it to a data source:
here u can extract a little info on a given exception:
protected void Application_Error( object sender, EventArgs e )
{
Exception Exc = null;
try
{
Exc = Server.GetLastError();
if(Exc.InnerException != null)
Exc = Exc.InnerException;
// Method name + line number + column
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(Exc, true);
string ExtraData = "Name : {0}, Line : {1}, Column : {2}";
ExtraData = String.Format(ExtraData, trace.GetFrame(0).GetMethod().Name, trace.GetFrame(0).GetFileLineNumber(), trace.GetFrame(0).GetFileColumnNumber());
// exception message
Exc.Message;
// page name
Request.Url.ToString();
// stack trace
Exc.StackTrace;
}
}
精彩评论