log4net: How To

Configurations that go into the configuration tag in the Web.Config(File Appender)



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


              <log4net debug=”false”>

             <appender name=”LogFileAppender” type=”log4net.Appender.FileAppender,log4net” >

                  <param name=”File” value=”D:LogRTS-log.txt” />

                       <param name=”AppendToFile” value=”true” />

                            <layout type=”log4net.Layout.PatternLayout,log4net”>

                           <param name=”ConversionPattern” value=”%d [%t] %-5p %c [%x] – %m%n” />                         




                        <priority value=”DEBUG” />

                         <appender-ref ref=”LogFileAppender” />




 Initialize the loggers configurations in Global.asax 

protected void Application_Start(Object sender, EventArgs e)




 Using the logger in a page

private static ILog log = LogManager.GetLogger(typeof(SLK.RTS.Web.UI.Review.ReviewersFeedback));

private void Upload()




              throw new Exception();


       catch(Exception ex) {

                 log.Fatal(“Upload failed” + ex.Message);





  Catching all logger exceptions

protected void Application_Error(Object sender, EventArgs e){   

        const string strUserId = “User Id : {0};n”;

        Exception ex = Server.GetLastError().GetBaseException(); 

        if(log.IsFatalEnabled) log.Fatal(String.Format(strUserId,Utility.AppContext.UserId.ToString()),ex);   

        if (log.IsInfoEnabled)log.Debug(ex.StackTrace);

        if (log.IsInfoEnabled) log.Info(“n———————————————-n”);


Restricted Class Access:CAS

(Namratha’s post on cochindotnet) 

 Managed code offers several ways to restrict method access:
  • Limit the scope of accessibility to the class, assembly, or derived classes, if they can be trusted. This is the simplest way to limit method access. Note that, in general, derived classes can be less trustworthy than the class they derive from, though in some cases they share the parent class’s identity. In particular, do not infer trust from the keyword protected, which is not necessarily used in the security context.
  • Limit the method access to callers of a specified identity — essentially, any particular evidence (strong name, publisher, zone, and so on) you choose.
  • Limit the method access to callers having whatever permissions you select.
Lets see how can we accomplish this.

1) Create a strong named assembly e.g. Calc.dll. with one class called MyClass having the Add() method which add two numbers.
2) .Net Framework has tool called SecUtil.exe.
3) Go to the Visual Studio command prompt
4) Type SecUtil.exe /? . This will display the help and all the available options.
5) Then type secutil.exe -s -hex -c Calc.dll (or the name of ur dll).
6) This will display the public key as hexadecimal value as shown below.

C:DotNetDLLProjbinDebug>secutil -hex -c -s  Calc.dll
Microsoft (R) .NET Framework SecUtil 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Public Key =
Name =
Version =

Now You can use this Public key with any class or method in your assembly that you don’t want anyone else to access, you can use the StrongNameIdentityPermissionAttribute for this. Any calling code that isn’t signed with your .snk file won’t have access to it.

Here I have used it at the class level. You can also achieve the same at method or assembly level.

So,Lets secure our class.

// put this code above the class as shown

   PublicKey = “0x0024000004800000940000000602000000240000525341310004000001000100D96FE3B963FC64″ +
  “B8A9B6CA05B859A67B8B30603A0D696E1F95D8C9B23C5B2EEF139B96A5CC55C2E38D05B7FD675434″ +
public class MyClass
   public MyClass()

   public int Add(int i , int j)

7) Now create any Win app which will be a client app for this assembly.
8) Do not srong name this assembly.
9) Reference the above assembly in the cleint App.
10) Call the Add method or any other method of the Myclass.
11) The code will compile.
12) Try and execute the function call . You will a similar error message.

Additional information: Request for the permission of type System.Security.Permissions.StrongNameIdentityPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.

Scroll Position in datagrid within a Div


In the ItemDataBound event of your grid add the following code to add
a number bookmark to each row in the grid that is created:
// placing a bookmark against the first column
LiteralControl anchor = new LiteralControl();
anchor.Text = “<a name=”” + itemCount.ToString() + “”>”;
itemCount ++;

Then in the EditCommand event method add the following code to get the
browser to jump to the item when the page reloads:
//*************EditCommand **************************
System.Text.StringBuilder jScript = new System.Text.StringBuilder();
jScript.Append(“<script language=”JavaScript”>”);

this.RegisterClientScriptBlock(“Bookmark”, jScript.ToString());
//************EditCommand *************************

Im not sure about smart navigation enabled with this