Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to create a reusable custom Project Template for VS.NET 2005

0.00/5 (No votes)
24 Apr 2007 1  
This article describes the basic procedures for creating a Project Template in VS.NET that can be reused as a custom template for other projects.

Introduction

This article outlines the procedures necessary for creating a custom Project Template in Visual Studio .NET 2005. As an example, it specifically shows how to create a customized "Logger" template, using the Apache Log4Net assembly. The example code was implemented with VB.NET.

Background

Your machine must have .NET Framework 2.0+ installed. Additionally, a recent build of the Log4Net assembly will be needed for the purposes of this example. Log4Net can be downloaded [here].

Using the code

In order to successfully export a project as a .NET Project Template, you must first make sure that the "Export Template" menu item is available from the "File" menu. If it is not present, it must be added through the IDE Options. This can be accomplished by clicking "Tools-->Customize..." and then the "Commands" tab. Within this tab, click on the "File" item on the left, then click the "Rearrange Commands..." button at the bottom. Click the "Add..." button at the top right, then click on "File" again in the Categories list, and "Export Template..." in the Commands list. I placed the command right above the "Save Selected Items" menu option:

Close out of the "Rearrange Commands" screen, and the "Customize" window. Your "File" menu should now contain the newly added command:

Now we can begin our project. For this example, we will create a Windows Application in VB.NET. Go to File -> New -> Project, and select "Windows Application" from the Visual Basic project types. I named the project WinAppWithLogging:

Now the necessary references must be made to utilize the Log4Net capabilities in our project. Add the following references to the project (right-click "References" in Solution View, and "Add Reference"):

  • System.Configuration
  • log4net.dll (this can be obtained by making a reference using the "Browse..." option and searching within the downloaded log4net package folder)

Next, add an application configuration file to the project by right-clicking the project in the Solution View, and then "Add-->New Item...". We will use this file to configure the log4net properties for the project.

Open the app.config file, and add the following code block below the <system.diagnostics> section (this is a basic configuration for setting up a regular file log, and rolling an appending log; for more detailed instructions on how to configure the log4net assembly, see the Appache log4net website):

<!-- This section contains the log4net configuration settings -->
<log4net Debug="false">
    <!--define the appenders-->
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
        <file value="App_logs\WinAppAppenderLog.txt" />
        <!-- Example using environment variables in params -->
        <!-- <file value="${TMP}\log-file.txt" /> -->
        <appendToFile value="true" />
        <!-- An alternate output encoding can be specified -->
        <!-- <encoding value="unicodeFFFE" /> -->
        <layout type="log4net.Layout.PatternLayout">
            <header value="[Start Log]" />
            <footer value="[End Log]" />
            <conversionPattern value="%date [%-5level] (%logger)- %message%newline" />
        </layout>
        <!-- Alternate layout using XML            
        <layout type="log4net.Layout.XMLLayout" /> -->
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="App_logs\RollingWinAppAppenderLog.txt" />
        <appendToFile value="true" />
        <maxSizeRollBackups value="250" />
        <maximumFileSize value="150000" />
        <rollingStyle value="Size" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <header value="[Start Log]" />
            <footer value="[End Log]" />
            <conversionPattern value="%date [%-5level] (%logger)- %message%newline" />
        </layout>
    </appender>
    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="RollingLogFileAppender" />
    </root>
</log4net>

Note that I've elected to have the application create the log files in a directory called App_logs, but they can be created anywhere in your application structure. The location is defined in the value attribute under the LogFileAppender and RollingLogFileAppender property configurations.

Lastly, we will need to add some initial code to our main form. Open Form1.vb, and add the following code to the class header:

Imports System.Configuration
Imports System.Reflection
Imports log4net

<Assembly: log4net.Config.XmlConfigurator(Watch:=True) /> 

Public Class MainForm

    Private log As ILog = _
      LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType)

    Private Sub MainForm_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load


        'to use log object:
        '
        'log.LogInfo("message to log...")
        '
        ''''''''''''''''''''''''''''''''
    End Sub

    ...

This will tell the compiler to use the Log4Net XMLConfigurator class to utilize the configurations defined in app.config.

Optionally, a separate class could be created to utilize the log object. I have included this method in the example source code. This would enable the object to be used globally throughout each form in the application code.

We can now build the project (Build-->Build WinAppWithLogging), to generate our new project assembly.

Now that we have our project built, we are ready to export it as a new Project Template. Simply click on "File-->Export Template..." and make sure that "Project Template" is selected:

Click "Next" on the Export Template wizard, and fill out the necessary boxes as needed. I left the default checkboxes, but you can enter a description for the template, as well as a custom icon that will appear in the project types list in Visual Studio.

Now click Finish. This will create a .zip file within the "My Exported Templates" directory, which will be used by the IDE to load the new template into the project types list.

To see and use the new Project Template, close out of Visual Studio and start it again. If you go to "File-->New-->Project..." and look in "Visual Basic" Project Types, you should see the new "WinAppWithLogging" project template:

You can now use this custom created template for any Windows application that requires file logging.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here