?
Solved

access public variable from Global.asax file

Posted on 2014-04-10
7
Medium Priority
?
1,971 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

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

Assisted Solution

by:Miguel Oz
Miguel Oz earned 1000 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
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 500 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 500 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 36

Accepted Solution

by:
Miguel Oz earned 1000 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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