|
|
|
|
|
|
|
Total page views :
3492
|
|
Total downloads :
49
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
Over the years I've had to write enough
batch jobs that I've come up with a template. Most batch jobs have a number of
common elements. When the job runs, you want it to record in a log the time
when it ran, what tasks it performed and the time when it finished. If the
batch job runs into problems, you want it to record the problem to the log and
then exit with an error code. Most batch jobs also need to connect to a
database.
Writing all the code to create a log, get a database
connection string and connect to the database is time consuming. It's best to
build something reusable. So without further ado, here is the template I use. Comments and explanation are at the end.
using
System;
using
System.Collections.Generic;
using System.Configuration;
using System.IO
using System.Text;
namespace ConsoleTemplate
{
class
BatchTemplate
{
public static
StreamWriter LogWriter =
null;
static int Main(string[]
args)
{
OpenLog();
db =
DatabaseFactory.CreateDatabase("MyDatabase");
DbCommand cmd =
db.GetStoredProcCommand("MyStoredProcedure");
WriteToLog(
"Hello
world!");
CloseLog();
return 0;
}
static private void
OpenLog()
{
// Create log folder if it
doesn't exist
string folderPath =
Path.Combine(Environment.CurrentDirectory,
"Log Files");
DirectoryInfo logFolder =
new
DirectoryInfo(folderPath);
if (!logFolder.Exists)
logFolder.Create();
// Clear out old logs
string appSetting =
ConfigurationManager.AppSettings["DaysToStoreLogFiles"];
int days =
int.Parse(appSetting);
TimeSpan span =
new
TimeSpan(days, 0, 0, 0);
foreach(FileInfo
logFile in
logFolder.GetFiles())
{
if (logFile.CreationTime <
DateTime.Now - span)
logFile.Delete();
}
// Create new log file string filePath = Path.Combine(folderPath, DateTime.Now.ToString("yyyy-MM-dd-hhmmss"));
if (File.Exists(filePath + ".txt")) { bool exitApplication = true; for (int x = 1; x <= 10; x++) { string newPath = filePath + "(" + x.ToString() + ")";
if (!File.Exists(newPath + ".txt")) { filePath = newPath; exitApplication = false; break; } }
// If someone has really launched the batch job more than 10 times in // a single second, something is wrong. if (exitApplication) Environment.Exit(1); }
LogWriter =
File.CreateText(filePath +
".txt");
WriteToLog("Begin
processing: " +
DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss"));
WriteToLog("-----------------------------------------------------------");
WriteToLog("");
}
static private void
CloseLog()
{
WriteToLog("");
WriteToLog("------------------------------------------------------------");
WriteToLog("Finish
processing: " +
DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss"));
LogWriter.Close();
}
static private void
WriteToLog(string logEntry)
{
LogWriter.WriteLine(logEntry);
}
}
}
The App.Config file:
<?xml
version="1.0"
encoding="utf-8"
?>
<configuration>
<configSections>
<section
name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<connectionStrings>
<add
name="MyDatabase"
connectionString="Data
Source=MYDATABASE;Initial Catalog=DEV;User ID=XYZ;Password=12345"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
<appSettings>
<add
key="DaysToStoreLogFiles"
value="60"/>
</appSettings>
</configuration>
Notes:
Every time the batch job runs, the first
thing it does is call OpenLog( ).
This method performs a number of tasks. The first thing it does is look for a
folder called Log Files. This folder should be in the same place as the batch
job executable. If it isn't there, it gets created. All the batch job log
files will be stored here.
The next task OpenLog( ) performs is to clear out old log
files. In the app.config file there is a setting which says log files should be
stored for sixty days. Log files older than sixty days are deleted.
Now OpenLog( ) creates a new log file named
[year-month-day-hour-minute-second].txt. If there is already a file with this
name (because the batch job was launched twice in the same second), the second
file will have a "(2)" added to the end of the file name.
Now that we have a folder to hold log files and we've
created a new log file for this run, OpenLog( ) records the time when processing
began.
CloseLog( ) records
the time when processing is complete and then closes the log file.
WriteToLog( ) writes
a string into the log file. Each string thus written goes on its own line in
the log.
The .config file also contains the entries for the
Microsoft Enterprise Library data access block and a connection string. With
these in place, the batch job is ready to connect to a database, as shown in the
Main( ) function.
After a successful run, the batch job exits with a return
code of 0. If it fails while creating the log, it exits with a return code of
1.
The batch job shown above creates a log file name
2009-11-28-110817.txt that looks like this:
Begin processing: 2009-11-28-09:08:17
-----------------------------------------------------------
Hello world!
------------------------------------------------------------
Finish processing: 2009-11-28-09:08:18
|
When you make your own batch
job, the first thing you do is call OpenLog(). The last thing you do when
you're done is call CloseLog(). In between, you can write to the log using the
WriteToLog(string) method. All that's left for you to do is add your own code.
Enjoy.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
Andrew Fenster
Andrew Fenster is an IT consultant in Cleveland, Ohio. He has worked as a developer, technical lead or architect on many projects, large and small. He has consulted for Progressive Insurance, Charles Schwab, OE Connection and National City Bank, among others.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|