简体   繁体   中英

How to get method name automatically in Code Snippet in C# 4.0

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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM