How to create log file from batch files

Many times we can do our tasks in batch mode. I personally like this approach and I use it alot in
my daily office work. In this post we will learn a very simple but very usefull trick to make our
batch files more usefull.

Scenario:
I have 3 batch files which have some operation in them (anything)
file1.cmd
file2.cmd
file3.cmd

I want to execute these batch files in one go and want to create a log file for actions happened inside the batch file.

Solution

1. Open ‘Notepad’ and create a new file. save this file as “Main,cmd” (you can choose nay name you like )
2. Create a variable which contains our Logfile name.
3. Copy an d paste below mntioned lines in this file and save it .

@echo off
 REM Create a logfile name variable
 SET LOGFILE=myLOG.LOG
 ECHO This is first log line >> %LOGFILE%
 ECHO Calling first file ‘file1.cmd’ >> %LOGFILE%
 REM file1.cmd >> %LOGFILE%
 ECHO Calling file ‘file2.cmd’ >> %LOGFILE%
 REM file2.cmd >> %LOGFILE%
 ECHO Calling file ‘file3.cmd’ >> %LOGFILE%
 REM file3.cmd >> %LOGFILE%
 ECHO Task completed >> %LOGFILE%
ECHO. >> %LOGFILE%

4. Run this file by double click on it .
Voilaa now you have new log file.

Note : If you have “Trace32” log viewer you can directly lauch it loaded with your log file
i.e.

START trace32.exe %LOGFILE%

How to use Log4Net

Hi , in today’s post , i am going to explaing a sample , to use log4net assembly , which is used for Logging and faster than Enterprise library.

follow the steps :

(1)Create a new web site project in VS200*.

(2)Add reffernce to the Log4net assembly to your website and add using log4net namespace.

(3) add a confguration file and name it myLogConfig.config.

(4)Add the  the following content to this file

<?xml version=”1.0″ encoding=”utf-8″ ?>
<log4net>
<root>
<level value=”DEBUG” />
<appender-ref ref=”RollingLogFileAppender”/>
</root>
<appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”>
<file value=”app.log” />
<appendToFile value=”true” />
<rollingStyle value=”Size” />
<maxSizeRollBackups value=”10″ />
<maximumFileSize value=”100KB” />
<staticLogFileName value=”true” />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%d [%t]%-5p %c [%x] – %m%n” />
</layout>
</appender>
<appender name=”RollingLogFileAppender” type=”log4net.Appender.RollingFileAppender”>
<file value=”app.log” />
<appendToFile value=”true” />
<datePattern value=”-dddd” />
<rollingStyle value=”Date” />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%d [%t]%-5p %c [%x] – %m%n” />
</layout>
</appender>
</log4net>

(5)Add global.ascx to  your website and add the following method

protected void ConfigureLogging()
{
string logFile = HttpContext.Current.Request.PhysicalApplicationPath + “log4net.config”;
if (System.IO.File.Exists(logFile))
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logFile));
}
}

 

(6)Call this method

void Application_Start(object sender, EventArgs e)
{
ConfigureLogging();

}

 

 

 

  (7) Now in your default.aspx.cs file add the following  code

log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
try
{
log.Debug(“In Page Load Function”);
log.Debug(“Going to generate divide by zero”);
int a = 0;
int b = 10 / a;
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}

 

Now run your application and have alook at the newly created log file .