2

In Visual Studio you can use code snippets e.g. when your are editing a class you can type ctor and the default constructor will automaticly be added to your class. Is it possible to create a code snippet in Visual Studio, that does the following:

  • Creates the get/set Logger property where the cursor is.
  • Adds using Castle.Core.Logging
  • Lets me choose where in the list of instance variables I can place 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:

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; }
    }
}
samy
  • 14,832
  • 2
  • 54
  • 82
aquaUrge
  • 53
  • 7
  • This SO question may contain some code you are seeking http://stackoverflow.com/q/5872131/122005 – chridam Aug 31 '12 at 12:53
  • That questions is about when the logger is injected by castle windsor. I want a code snippet for Visual Studio. In Visual Studio when you are editing a class you can type `ctor` and the default constructor is added to the class. I want to type `logger` and have the logger code added to my class. – aquaUrge Aug 31 '12 at 16:34

1 Answers1

0

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; this is an XML file that includes the code you want to add, and references. The basic example includes all of the requirements except shortcut.
  2. Import the code snippet into your visual studio instance.
  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>
M Afifi
  • 4,645
  • 2
  • 28
  • 48