Friday, February 18, 2011

Julian Date

I was a little busy over the past week with lots of traveling, It's been a productive week for me personally and my next post would be on my experiences with LINQ to XML definitely. I got fascinated with LINQ for a long time finally my project requirements have necessitated the need to use LINQ but current post is about a nifty little feature which i made use of yesterday and i figured someone would make use of this feature in case if they were searching for it.

I am a lazy guy, laziness has always been my trademark niche and i tend to spend more time exploring the framework functionality to get the job done real quick and it's not that i am against development of custom methodologies, I am an ardent lover of custom applications. I personally like accomplishing things this way rather. :)

The requirement i faced was a minor part of my module which needed the Current time object to be Converted to the Julian date format [YYDDD] for instance today February 18 2011 can be represented as 11049 in string notation. 

11- Last 2 digits of year 2011
049 - Represents the Current Number of this day in the calendar year from 0-365 (31 days in January + 18 days currently)

It's a straightforward simple routine and It's in C#

public string RetJulianDate(DateTime dt)
        {
            DateTime dObj = dt;
            String sYear = dObj.Year.ToString();
            int month = dObj.Month;
            int day = dObj.Day;
            //Current Month difference
            int current = day;
            
            int sum = 0;
            int final = 0;
            if (month > 1)
            {
                for (int i = 1; i < month; i++)
                {
                    sum += DateTime.DaysInMonth(dObj.Year, i);
                }

                final = sum + current;

            }
            else
                final = current;
            return (sYear.Substring(1, 2) + final.ToString("000"));
        } 
 
 
 
Until Next time! 

Friday, February 4, 2011

Logging - Oh ya Log4Net

One of the most important aspects to a good application in fact one of the most pivotal components happens to be logging. If not for logging it would be very nasty to troubleshoot an issue when the application goes live onto production. Where do you start looking for problems??

There are some really awesome logging frameworks out there but I am going to focus on an Open Source logging framework which i use it with .Net viz Log4Net. It's very easy to incorporate this feature onto your application and the best part it's free. Log4net is part of Apache Logging Services project which was intended to provide cross language logging services primarily for debugging.

I think I've given enough funda for now and i am going to jump into the essentials which one needs to do to incorporate Log4net as part of their application in this post. I was actually brooding over setting up this for my application 3 months ago and when i looked up i found most of the articles i read about setting this up were not properly documented. I decided to make a post about this properly so it might help someone in the future when they read this.

Before i start developing the sample application. Log4net can be downloaded from here, You can get the latest Log4net build from this site. It contains the Log4net dll which will be referencing for the sample application i develop.

I will explain this with a simple Forms application in Visual Studio. Create a Forms application on Visual Studio and once the application is created, Add a new config file to the application namely App.config and i think the abstraction would be the scenario that application would have a config file and in case it has many modules all the modules in turn would have their App.config files seperately. Before you use log4net you need to create a configuration setting for it in App.config file. I've listed the App.config setting below


  
    

The main thing to note is the param tag by name File. This contains the destination where the log file will be created for instance in this scenario it will be created under the folder MyLogFolder with the log file name as customlog.txt. Now i will focus onto the Sample Application part and how to go about accessing the log4net from the application side. The application like i mentioned is a simple forms application with a button click event as shown in the snippet below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;

namespace Log4Net
{
    public partial class Form1 : Form
    {

        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  
        public Form1()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log.Warn("Custom Warning Message");
            log.Debug("Custom Debug Message");
            log.Info("Custom Info Message");
            log.Error("Custom Error Message");
        }


    }
}


The code snippet is very elementary. It shows the way to incorporate the log4net after adding the reference to the DLL via the application. The main thing to note would be lines 17 & 21 which are both self explanatory in nature and on the click of the button the log file would've have the following contents as shown below.

WARN 2011-02-04 12:03:07 – Custom Warning Message
DEBUG2011-02-04 12:03:07 – Custom Debug Message
INFO 2011-02-04 12:03:07 – Custom Info Message
ERROR2011-02-04 12:03:07 – Custom Error Message


Like i said it's just a skeleton and it's a very simple framework with very strong functionality embedded in it. Logging made easy!!

Jet Set Go

I had this blog created for a long time ago, the primary reason i wanted it to be the avenue to share my nerdy thought processes sometimes (Don't worry it wont be that nerdy i try to keep it really simple enough). i had this posts in drafts for over 2 months and now that I've finally decided to curb my procrastination and wanted to nail it down today.

I've been in the blogging circle for a while ever since the completion of my masters i had this fascination to start my own tech venture where i could share my new eureka moments which i get at work but deep down if i look at it intuitively the complex issues are the ones which can be solved easily but most of the time is spend on some trivial issues which we tend to overlook and most of the ventures out here would be to focus on the trivial issues and how to go about solving it :)

My normal blog would be this always but i will move some of the aspect to this part. This would be the avenue for the bigger things i plan to venture into this year hopefully. In case you were wondering about the part why i used GlumLicense as the blog post name well i started this blog as soon as i got my new 360 and Glumlicense was my Xbox Gamertag then (Just to let you know how lazy i really had been or may be it's my Xbox) right now you can always connect to me easily via my Gamertag which is machovenki.


The Stage is set. Let the Game begin!!!