如何在C#4.0的代码片段中自动获取方法名称

5 .net c#

我正在使用C#在ASP.Net 4.0中开发一个应用程序,我正在使用Microsoft的Enterprise Lib进行日志记录和跟踪.我的问题是几乎在每个函数中,根据我公司的guidline,与数据库或一些关键业务规则进行交互,使用这种格式的跟踪.

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());
}
Run Code Online (Sandbox Code Playgroud)

所以我想要的是将它转换为自动检测当前方法的代码片段,比如上面我们有XYZ()的情况.

请帮我.还告诉我将其添加到intellisense的方法.现在我可以创建.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>
Run Code Online (Sandbox Code Playgroud)

例如,如果我的方法是

void XYZ()
{
  // when i insert snippet here , the snippet will automatically detect 
Run Code Online (Sandbox Code Playgroud)

该片段将被置于XYZ函数下.

try
                {
                    _traceLog.Clear();
                    _traceLog.AppendLine("XYZ started: XYZ()");   
Run Code Online (Sandbox Code Playgroud)

//在旁边的附加行我想得到方法的名称,我在其中放置代码片段.

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

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

}
Run Code Online (Sandbox Code Playgroud)

这可能吗?或者我必须手动输入或以其他任何方式输入?

Dar*_*rov 7

MethodInfo.GetCurrentMethod().Name将为您提供当前正在执行的方法的名称.警告:匿名方法会给你垃圾的名字.


Wra*_*ath 4

我用这个:

try
{

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

这是该片段的代码:

<?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>
Run Code Online (Sandbox Code Playgroud)

请注意其中包含“try”的快捷方式标签。
这意味着我可以通过在 Visual Studio 中键入try插入代码片段来使用代码片段,或者使用 Ctrl+k、Ctrl+S(默认键盘快捷键)标记一段代码并用此代码片段包围它