【问题标题】:Application state asp.net应用程序状态 asp.net
【发布时间】:2021-11-08 12:29:18
【问题描述】:

我在 global.asax 文件的 Application_Start() 方法中编写了以下代码。在这段代码中,我在应用程序状态中保存了一个名为 allowedLogTypes 的列表

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

我的问题是我无法访问 LogHelper.cs 文件中的应用程序状态

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


    }
}
  

【问题讨论】:

  • 当您可以将应用程序状态作为参数传递给WriteLog 方法时,为什么还要创建对应用程序状态的依赖项?

标签: c# asp.net c#-4.0 asp.net-4.0 application-state


【解决方案1】:

在class文件中访问Application状态,需要添加HttpContext.Current引用。例如:HttpContext.Current.Application["AllowedLogTypes"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 2016-07-31
    • 2011-10-15
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多