Saturday, 18 February 2017

Log4net is not working on production machine, How to fix that?

Log4net is commonly used tool to log custom exceptions, same is in my current project. It is useful to write trace information or exception. By the way for un handled exceptions i am using Elmah which is doing pretty well.

Problem

We have smart deployment system to our test and development servers, but currently issue was that locally log4net works pretty cool but on production it does not. To configure I am using assembly attribute 
[assembly: log4net.Config.XmlConfigurator(Watch=true)]

Solving

Firstly i thought issue is with permission because file was not created, i added iis_iusrs and finally everyone with full permissions but it does not help.
Finally i started reading log4net faq and found interesting topic about release configuration which says 
For a command line application "as early as possible" probably is the class holding the Main method, for a Web-Application it would be your Global.asax class and for a Windows Service it would be the class deriving from ServiceBase.
Going further i found other topic which says
Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call toLogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.
So i added code to global.asax

  protected void Application_Start()
        {
           var logger = LogManager.GetLogger("Default");        }
And finally everything works on all my servers. I can log my trace info, which helps me to solve issues if any.

log4net not working on production server

This is the log4net configuration in the web.config of the website.


<configuration>
      <log4net>
        <root>
          <level value="DEBUG" />
          <appender-ref ref="LogFileAppender" />
        </root>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="log.txt" />
          <param name="AppendToFile" value="true" />
          <rollingStyle value="Size" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="30MB" />
          <staticLogFileName value="false" />
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
          </layout>
        </appender>
      </log4net>
    </configuration>
I also have a class library and this is its App.config file


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="30MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>

This is how I call the log function on every class:
private static readonly ILog log = LogManager.GetLogger(typeof(AppDomain));
...and this is how i call it :
log.Error("\n\t=>" + ex.GetBaseException().Message + "\n\r" + " @ " + Environment.StackTrace);

Wednesday, 11 January 2017

switch in c#

switch (ds.Tables[0].Rows[0]["STATE"].ToString())
                        {
                            case "CA": strPhone = "(877) 702-6666"; break;
                            case "CO": strPhone = "(877) 730-6666"; break;
                            case "NV": strPhone = "(877) 782-6666"; break;
                            case "UT": strPhone = "(877) 950-6666"; break;
                            case "AZ": strPhone = "(877) 272-6666"; break;
                            case "TX": strPhone = "(877) 575-6666"; break;
                        }

Tuesday, 3 January 2017

Javascript Date Mask with mm/dd/yyyy format in text box

onkeyup="return fnDateMask(event,this);"

function fnDateMask(event,control) {          
        var KeyID = event.keyCode;
        if (KeyID != 8)
        {    
            var s = new String(control.value);
            if (s.length == 2 || s.length == 5) {
                control.value = s + "/";
            }
        }
    } 

Javascript Number Validation on Key Press

onkeypress="return isNumber(event)"

function isNumber(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    else
        return true;
}   

Saturday, 3 December 2016

Log4Net - Logging in to Log text File in Asp.net Application step by step

FrameWorkUsed in this application 2.0

Step 1 : Click here to Download Log4net Dll from official site.

Step 2 : Unzip the Downloaded Zip File. Navigate to this path .
C:\Users\Gokul\Downloads\Compressed\log4net-1.2.15-bin-newkey_2\log4net-1.2.15\bin\net

Step 3 : Select your asp.net framework version folder and click on release folder ,

C:\Users\Gokul\Downloads\Compressed\log4net-1.2.15-bin-newkey_2\log4net-1.2.15\bin\net\2.0\release

and copy log4net.dll from that folder and paste it in the bin folder of your applciation.

For Ex : C:\Users\Gokul\Documents\visual studio 2010\Projects\Log4net2.0\Log4net2.0\bin

Step 4 : Now add reference of log44Net dll to your applcaition by  ,

Right Clicking on you Project , Add Reference > click on Browse  .

Navigate to the path of Bin where You pasted the Log4Net dll.
For Eg : Here C:\Users\Gokul\Documents\visual studio 2010\Projects\Log4net2.0\Log4net2.0\bin

Select the Log4net dll from that path and then click ok to the Log4net dll Reference to our Project.

Step 5 : Now in Your Assemblyinfo.cs File , add this line of code

[assembly: log4net.Config.XmlConfigurator(Watch = true)] as showed in image Below.



Step 6 :
Now add this line of Code in your Global.asax in the Application_Start

 protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

         }

Step 7 : Now in web.config file add this code in , <configuration> Section

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  </configSections>
<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:/log.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.SimpleLayout"/>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>

  </log4net>

Like below in my Web.cofig file ,



Step 7 : In the web pages where you want to Log your Information of the user activities .
Add this Line of code , as below in image of my sample Default.aspx page.

 private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);



log.Info(" Your Message here to log when user visited this page .");


Thursday, 3 November 2016

Javascript validation for Date Of Birth

function ValidateDate() {
        var input = document.getElementById('<%= txtAppDOB.ClientID %>').value;
        var pattern =/(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;
        var ss=pattern.test(input).toString();

        if(ss=="false")
        {
            alert(Invalid Dob);
        }
        else
        {
          alert(Valid Dob);
        }