0

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);
                });
            }
        }


    }
}
  
Slow Death
  • 31
  • 3
  • Why create a dependency to application state when you can pass it as a parameter to the `WriteLog` method? – iamdlm Sep 13 '21 at 09:35

1 Answers1

0

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

Malvik Bhavsar
  • 407
  • 5
  • 8