[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

access public variable from Global.asax file

Posted on 2014-04-10
7
Medium Priority
?
2,026 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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