Application state asp.net

67 views Asked by At

I have written below code in Application_Start() method of global.asax file. In this code i am saving a list by the name allowedLogTypes in the Application state

        Application["user"] = 0;
        List<string> allowedLogTypes = new List<string>();
        string logTypes= System.Configuration.ConfigurationManager.AppSettings["LogType"];
        List<string> lstLogTypes = logTypes.Split('|').ToList<string>();
        if (lstLogTypes.Contains(LogTypes.DEBUG_LOG))
        {
            allowedLogTypes.Add(LogTypes.DEBUG_LOG);
        }

        if (lstLogTypes.Contains(LogTypes.INFO_LOG))
        {
            allowedLogTypes.Add(LogTypes.INFO_LOG);
        }

        if (lstLogTypes.Contains(LogTypes.WARN_LOG))
        {
            allowedLogTypes.Add(LogTypes.WARN_LOG);
        }

        if (lstLogTypes.Contains(LogTypes.WARN_LOG))
        {
            allowedLogTypes.Add(LogTypes.WARN_LOG);
        }
        **Application["AllowedLogTypes"] = allowedLogTypes;**
        GlobalConfiguration.Configure(WebApiConfig.Register);

My issue is that I cannot access the Application state inside the LogHelper.cs file

namespace Reporting.Helper
{
    public class LogHelper
    {

        public static void WriteLog(ApiInfo apiInfo, MethodBase methodBase, string LogType, Exception ex, string logMessage)
        {
            List<string> allowedLogTypes = **Application["AllowedLogTypes"];**
            if (allowedLogTypes.Contains(LogType))
            {
                var logTime = UtilityHelper.CurrentDateTime();
                ThreadPool.QueueUserWorkItem(task =>
                {
                    PushAppLogInDB(methodBase, apiInfo, logTime, LogTypes.DEBUG_LOG, logMessage, string.Empty, ex);
                });
            }
        }


    }
}
  
1

There are 1 answers

0
Malvik Bhavsar On

Accessing the Application state in the class file, need to add HttpContext.Current reference. E.g: HttpContext.Current.Application["AllowedLogTypes"]