5

I am developing an application in ASP.Net 4.0 with C# where I am doing logging and tracing using Microsoft's Enterprise Lib. My problem is that almost in every function , as per my company guidline, where there is interaction with database or some critical business rules , use tracing, in this format.

try 
{
_traceLog.clear();
_traceLog.AppendLine("XYZ method started: XYZ()");

_traceLog.AppendLine("XYZ method completed: XYZ()");
}
catch(Exception ex)
{
 _userException.CreateExceptionLog(ex);
}
finally
{
 _userException.CreateTraceLog(_traceLog.ToString());
}

So what i want is to convert it as a code snippet that will automatically detect the current method, say in above case we have XYZ().

Please help me. Also tell me the way to add this to intellisense. Right now i am able create .snippet file and uses insert snippet from context menu.

UPdate

I think i am not clear to you guys. Let me make more clear. i have an code snippet

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2010/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>
                TL
            </Title>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[ try
            {
                _traceLog.Clear();
                _traceLog.AppendLine("");   // here in side the append line i want to get name of method under which i am placing code snippet.
                _traceLog.AppendLine("");
            }
            catch (Exception ex)
            {

                _userExceptionLog.CreateExceptionLog(ex);
            }
            finally
            {
                _userExceptionLog.CreateTraceLog(_traceLog.ToString());
            }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

For example if my method is

void XYZ()
{
  // when i insert snippet here , the snippet will automatically detect 

that this snippet is going to be placed under XYZ function.

try
                {
                    _traceLog.Clear();
                    _traceLog.AppendLine("XYZ started: XYZ()");   

// here in side the append line i want to get name of method under which i am placing code snippet.

                _traceLog.AppendLine("XYZ completed: XYZ()");
                }
                catch (Exception ex)
                {

                    _userExceptionLog.CreateExceptionLog(ex);
                }
                finally
                {
                    _userExceptionLog.CreateTraceLog(_traceLog.ToString());
                }

}

Is this possible? or I have to enter it manually or any other short way?

3
  • 1
    I don't know the answer to your specific question, but have you considered using PostSharp instead? Commented Jan 19, 2011 at 10:39
  • 1
    I would suggest you suggest that logging, but not handling (ie. continue as if it didn't happen) every exception is not a good idea. Commented Jan 19, 2011 at 11:37
  • Richard is right. You are 'swallowing' exceptions, which is very bad practice. A better approach is to don't log at all at that level and letting exceptions bubble up, and log them at the root level of the application (for instance, in the Application_Error event of the global.asax of we ASP.NET web application). Commented Jan 19, 2011 at 11:47

2 Answers 2

7

MethodInfo.GetCurrentMethod().Name will give you the name of the currently executing method. Caveat: anonymous methods will give you garbage as name.

Sign up to request clarification or add additional context in comments.

2 Comments

thax Darin. I need one more thing , any other link or tutorial on Code Snippets , other than MSDN one.
"garbage" - it is still unique and you can use it to get the owning type.
4

I use this:

try
{

}
catch ( Exception ex )
{
    My.API.ErrorHandler.Handler.HandleError( ex, 
        System.Reflection.MethodBase.GetCurrentMethod().Name, 
        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName );
}

Here is the code for the snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Try Catch</Title>
      <Shortcut>try</Shortcut>
      <Description>Places a try catch block with My API error handling</Description>
      <Author>Nathan Freeman-Smith</Author>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[            try
            {

            }
            catch ( Exception ex )
            {
                My.API.ErrorHandler.Handler.HandleError( ex, System.Reflection.MethodBase.GetCurrentMethod().Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName );
            }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Note the Shortcut tag with "try" inside of it.
It means I can use the snippet by typing try in visual studio to insert the snippet, or, marking piece of code and surround it with this snippt by using Ctrl+k, Ctrl+S (Deafult Keyboard Shourtcut)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.