You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.7.2 Web Forms栈追踪不显示行号及VS设置重置问题

C# Web应用栈追踪行号显示及VS设置保存问题解决

一、Azure App Service中栈追踪显示行号的解决步骤

  • 确认PDB文件已部署
    右键项目→发布→编辑发布配置→设置→文件发布选项,勾选“在发布期间保留符号(PDB)文件”;也可以直接登录Azure门户,检查App Service的wwwroot目录下是否存在对应程序集的.pdb文件。
  • 添加Azure应用配置项
    进入Azure App Service的配置→应用程序设置,新增或修改两个配置:
    • DOTNET_SYSTEM_DIAGNOSTICS_TRACESOURCE_STACKTRACES_ENABLESOURCEFILEINFO:设为true,开启源文件信息显示
    • ASPNETCORE_ENVIRONMENT:生产环境设为Production(需确保日志配置允许输出完整栈追踪),测试环境可设为Staging
  • 修正全局异常处理逻辑
    如果是ASP.NET Web Forms项目,在Global.asax的Application_Error事件中,获取完整异常信息:
    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError().GetBaseException();
        // 此处将ex.StackTrace写入日志
        Server.ClearError();
    }
    

二、Visual Studio 2022中Debug设置自动恢复的解决步骤

  • 直接编辑项目文件
    右键项目→卸载项目→编辑.csproj文件,确保所有配置节点下的<DebugType>为pdbonly:
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
    </PropertyGroup>
    
    保存后重新加载项目,设置就不会自动重置了。
  • 检查解决方案配置
    点击顶部“生成”→“配置管理器”,确认每个项目的对应配置下Debug类型设置正确,避免解决方案级配置覆盖项目设置。
  • 清理重建项目
    执行“生成”→“清理解决方案”,再“重建解决方案”,清除旧编译缓存。

当前栈追踪示例:

System.NullReferenceException: Object reference not set to an instance of an object. 
at MyApp.SiteMaster.master_Page_PreLoad(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Page.OnPreLoad(EventArgs e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

期望显示效果:

System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.SiteMaster.master_Page_PreLoad(Object sender, EventArgs e) 
in C:\code\MyApp\project\MyApp\Site.Master.cs:line 64
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Page.OnPreLoad(EventArgs e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

内容的提问来源于stack exchange,提问作者GDR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:02:35