Embed pdb into assembly

后端 未结 3 1537
悲&欢浪女
悲&欢浪女 2021-02-07 12:20

I want my application to be distributable as a single .exe file but I want to be able to get nice error reports with source code line numbers (the application simply se

相关标签:
3条回答
  • 2021-02-07 12:37

    You could write a stub executable, that contains as embedded resources, both your actual executable and its pdb file. Upon starting the staub executable, it extracts the real executable and the pdb into a temporary directory and launches it.

    Just like some Installers or other applications do.

    I'm not sure if it is worth the extra effort though.

    0 讨论(0)
  • 2021-02-07 12:46

    Use MiniDumps instead of "exception.ToString()". It will give you a lot more information and does not need the .pdb to be distributed with the .exe.

    Useful Link: Post-Mortem Debugging Your Application with Minidumps and Visual Studio .NET

    0 讨论(0)
  • 2021-02-07 12:54

    I have used the following AssemblyResolve handler and then embedding both dll and pdb of any assemblies. You could set this up as anything that runs first thing in the application before dependencies are needed.

        private static void SetupEmbeddedAssemblyResolve()
        {
            // Based on http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                var name = args.Name;
                var asmName = new AssemblyName(name);
    
                // Any retargetable assembly should be resolved directly using normal load e.g. System.Core issue: 
                // http://stackoverflow.com/questions/18793959/filenotfoundexception-when-trying-to-load-autofac-as-an-embedded-assembly
                if (name.EndsWith("Retargetable=Yes"))
                {
                    return Assembly.Load(asmName);
                }
    
                var executingAssembly = Assembly.GetExecutingAssembly();
                var resourceNames = executingAssembly.GetManifestResourceNames();
    
                var resourceToFind = asmName.Name + ".dll";
                var resourceName = resourceNames.SingleOrDefault(n => n.Contains(resourceToFind));
    
                if (string.IsNullOrWhiteSpace(resourceName)) { return null; }
    
                var symbolsToFind = asmName.Name + ".pdb";
                var symbolsName = resourceNames.SingleOrDefault(n => n.Contains(symbolsToFind));
    
                var assemblyData = LoadResourceBytes(executingAssembly, resourceName);
    
                if (string.IsNullOrWhiteSpace(symbolsName))
                { 
                    Trace.WriteLine(string.Format("Loading '{0}' as embedded resource '{1}'", resourceToFind, resourceName));
    
                    return Assembly.Load(assemblyData);
                }
                else
                {
                    var symbolsData = LoadResourceBytes(executingAssembly, symbolsName);
    
                    Trace.WriteLine(string.Format("Loading '{0}' as embedded resource '{1}' with symbols '{2}'", resourceToFind, resourceName, symbolsName));
    
                    return Assembly.Load(assemblyData, symbolsData);
                }
            };
        }
    
        private static byte[] LoadResourceBytes(Assembly executingAssembly, string resourceName)
        {
            using (var stream = executingAssembly.GetManifestResourceStream(resourceName))
            {
                var assemblyData = new Byte[stream.Length];
    
                stream.Read(assemblyData, 0, assemblyData.Length);
    
                return assemblyData;
            }
        }
    
    0 讨论(0)
提交回复
热议问题