简体   繁体   English

适用于ILogger的Castle Windsor Visual Studio代码段

[英]Castle Windsor Visual Studio Code Snippet for ILogger

In Visual Studio you can use code snippets eg when your are editing a class you can type ctor and the default constructor will automaticly be added to your class. 在Visual Studio中,您可以使用代码段,例如,在编辑类时,可以键入ctor ,默认构造函数将自动添加到您的类中。 Is it possible to create a code snippet in Visual Studio, that does the following: 是否可以在Visual Studio中创建代码段,以执行以下操作:

  • Creates the get/set Logger property where the cursor is. 在光标所在的位置创建get / set Logger属性。
  • Adds using Castle.Core.Logging using Castle.Core.Logging添加
  • Lets me choose where in the list of instance variables I can place private ILogger _logger = NullLogger.Instance; 让我选择在实例变量列表中可以放置private ILogger _logger = NullLogger.Instance; .
public class Person
{
    private string name;
    private int age;

    public Person()
    {
    }

    // cursor is here and you type "logger"
}

After you type logger visual Studio adds the following code: 键入logger Visual Studio将添加以下代码:

using Castle.Core.Logging; // Added by code snippet

public class Person
{
    private string name;
    private ILogger _logger = NullLogger.Instance; // Added by code snippet
    private int age;

    public Person()
    {
    }

    // Added by code snippet
    public ILogger Logger
    {
        get { return _logger; }
        set { _logger = value; }
    }
}

There is two great tutorials here How to: Create a Basic Code Snippet & How to: Manage Code Snippets 这里有两个很棒的教程:如何:创建基本代码片段以及如何:管理代码片段

The summary is as follows, 总结如下,

  1. Create a .snippet file; 创建一个.snippet文件; this is an XML file that includes the code you want to add, and references. 这是一个XML文件,其中包含您要添加的代码和参考。 The basic example includes all of the requirements except shortcut. 基本示例包括快捷方式以外的所有要求。
  2. Import the code snippet into your visual studio instance. 将代码段导入到Visual Studio实例中。
  3. Use to your hearts content 用心去满足

Happy to throw together an example if you'd like. 如果愿意,可以举一个例子。

As I've created the snippet to test this, you'll want something as follows. 在创建代码段进行测试时,您将需要以下内容。 Self evident what you need to edit to suit your needs. 不言而喻的是,您需要根据自己的需要进行编辑。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2010/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Log4Net instance using Castle.Core.Logging
      </Title>
      <Shortcut>logger</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>Castle.Core.Logging.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>Castle.Core.Logging</Namespace>
        </Import>
      </Imports>
      <Code Language="CSharp">
        <![CDATA[private ILogger _logger = NullLogger.Instance;
        public ILogger Logger
    {
        get { return _logger; }
        set { _logger = value; }
    }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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