Solved

access public variable from Global.asax file

Posted on 2014-04-10
7
1,821 Views
Last Modified: 2014-04-11
Hi all,

I am implementing log4net to log all errors on my application.

Now i want to be able to easily log from any page in may site. So I have create the following variable in my GLobal.asax file

<script runat="server">
    public static readonly log4net.ILog log = log4net.LogManager.GetLogger
        (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Open in new window


Now when I try to use Global.log I am getting 'Global' does not exist in the current context?

any ideas why?
0
Comment
Question by:flynny
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 39991417
please try only with static variable (no readonly)
0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 39991446
Actually there is no need to do this. Just create a static global class with your logger property as per link below:
http://www.dotnetperls.com/global-variables-aspnet
0
 

Author Comment

by:flynny
ID: 39991625
thanks for the replies guys.

@jayakrishnabh - would Global still not be recognised in this case though?

@maz_oz2003 - ok so you are suggesting i create a class such as;

using System;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Contains my site's global variables.
/// </summary>
public static class Global
{
    public static readonly log4net.ILog log = log4net.LogManager.GetLogger
        (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}

Open in new window


obviously referencing the log4net dll, or would I still need to add get methods?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 125 total points
ID: 39991767
Hope your Global.asax file already contains Global class and this is where you are adding the public static variable inside that class. If yes, you can access that variable anywhere in the application using statement Global.VARIABLE_NAME. In this case you neednot create one more class for adding a variable which is already available with Global class. .Net allows this.  
But the suggested way would be not to alter global.asax and create a new static class for having the logger variable or its implementation.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 39991970
The problem is that you are not declaring the variable in code-behind, but rather in the markup file for Global.asax (note the <script> tag). Markup files in ASP.NET actually inherit from a class in the code-behind file:

e.g.

<%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication19.Global" Language="C#" %>

This means they are themselves classes. Basically, you are using the wrong class name to get to the variable. However, I don't think you can include any additional tags in the markup for Global.asax. So my question to you would be:  Why are you trying to declare the log4net stuff in the markup file as opposed to the code-behind file?

P.S.

readonly has nothing to do with why you cannot access the variable.
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 39993068
Is your application a web app or web site project?
For web app projects VS generates the code behind and a namespace (the default namespace in your project properties ->app tab) and kaufmed's explanation is correct for this case. You can access your variable as:
WebApplication19.Global.log
where WebApplication19 = default namespace.

Sadly for web site projects, you can not access global class like that, thus you need to create your own static class as you just did in your post (I will probably called SiteGlobal just to avoid any confusion with the global class)
0
 

Author Closing Comment

by:flynny
ID: 39993776
excellent thank you for all your help.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 37
exporting html table data 4 27
Could not load file or assembly 'Microsoft.Web.Infrastructure 4 33
SSIS Start 2 20
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question