Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / XML

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

Rate me:
Please Sign up or sign in to vote.
4.31/5 (9 votes)
24 Apr 2007CPOL4 min read 54.5K   411   45   3
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:

Image 1

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

Image 2

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:

Image 3

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)

Image 4

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.

Image 5

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):

XML
<!-- 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:

VB
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:

Image 6

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:

Image 7

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Matt Sciotto is an IT professional, who specializes in VB and .Net software/web application development. He has written several embeddable ActiveX COM controls, as well as custom server controls in VB.Net, and many scalable webform applications. In addition, he has a handy background in server scripting including knowlege of PHP,Perl,CGI and ASP, and Database Management with intense SQL foundation in MS SQL Server, MS Access, Oracle and MySql environments. He has worked on several OS platforms including all recent versions of Windows, Red Hat Linux, FreeBSD, UNIX and Solaris.

Comments and Discussions

 
GeneralGrayed out Export Template Pin
Danny Hutch26-Jun-07 13:47
Danny Hutch26-Jun-07 13:47 
AnswerRe: Grayed out Export Template Pin
Andromeda Shun28-Jul-08 4:08
Andromeda Shun28-Jul-08 4:08 
For C++ the template mechanism is different. Follow the note on this page[^].

Best regards,
Shun
GeneralGreat! Pin
Bjorn van der Neut8-May-07 23:02
Bjorn van der Neut8-May-07 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.