发布网站时,会话和应用程序事件不会触发

时间:2014-06-11 19:06:59

标签: c# asp.net webforms

这个问题有点问,但答案对我来说并不太清楚。 我使用应用程序和会话事件来编写日志并执行清理。 当我调试ap时,一切都在VS中工作,但是当我在服务器上部署到IIS时,事件不会被触发。我没有在Session_End或Application_End中使用请求,响应或服务器调用。我将模式设置为InProc。总而言之,这些过程都有效,但在发布时却没有。 有人提到了添加App_global.asax的问题​​,但我不太确定我应该在那里放什么代码

3 个答案:

答案 0 :(得分:1)

问题可能出在您的配置中。 App_global.ascx.compiled在编译时创建。您可以尝试创建新的serrings配置文件。只需转到配置管理器,添加新配置文件,然后在发布站点时,请检查预编译。

答案 1 :(得分:0)

确保您对IIS以及为日志指定的路径具有写入权限。

答案 2 :(得分:0)

好的,这是代码示例:

 void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
        {
            Path="~/Scripts/jquery-1.11.0.js"
        });
        Application.Add("logPath", Server.MapPath("Log"));
        System.IO.FileStream logFile = new System.IO.FileStream((String)Application["logPath"] + System.IO.Path.DirectorySeparatorChar + "log.txt",
                System.IO.FileMode.Append, System.IO.FileAccess.Write);
        System.IO.StreamWriter logWriter = new System.IO.StreamWriter(logFile);
        logWriter.WriteLine(">>>>>> Application started on " + DateTime.Now.ToString());
        logWriter.Close();
        logFile.Close();
    }

Page_Unload存储Session变量中的临时文件列表。 Session_End应该获取Session变量引用的列表和删除文件,但是出于这个问题的目的,让我只提出这个样本:

void Session_Start(object sender, EventArgs e) 
    {
     System.IO.FileStream logFile = new System.IO.FileStream((String)Application["logPath"] + System.IO.Path.DirectorySeparatorChar + "log.txt",
                System.IO.FileMode.Append, System.IO.FileAccess.Write);
        System.IO.StreamWriter logWriter = new System.IO.StreamWriter(logFile);
        logWriter.WriteLine("---- Sessoion started on " + DateTime.Now.ToString());
        logWriter.Close();
        logFile.Close();
    }
void Session_End(object sender, EventArgs e) 
    {
        System.IO.FileStream logFile = new System.IO.FileStream((String)Application["logPath"] + System.IO.Path.DirectorySeparatorChar + "log.txt",
                System.IO.FileMode.Append, System.IO.FileAccess.Write);
        System.IO.StreamWriter logWriter = new System.IO.StreamWriter(logFile);
        logWriter.WriteLine("---- Sessoion ended on " + DateTime.Now.ToString());
        logWriter.Close();
        logFile.Close();
    }

所有这些在VS和IIS Express中的本地计算机上运行正常。它在发布时不起作用。如果我只能在服务器上使用这个确切的代码,我可以添加实际的生产代码。

相关问题