Solved

access public variable from Global.asax file

Posted on 2014-04-10
7
1,781 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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