Solved

access public variable from Global.asax file

Posted on 2014-04-10
7
1,712 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now