|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Download WinAppWithLogging.zip - 155.3 KB
IntroductionThis 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. Example code was implemented with VB.Net. Background (optional)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 codeIn Order to sucessfully 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:
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, 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 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 to below the <!-- 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 newly 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||