Embed pdb into assembly

旧时模样 提交于 2020-01-12 04:30:06

问题


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 sends email with exception.ToString() and some additional information when unhandled exception occurs).

Is there any way to embed .pdb into assembly?


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/1518073/embed-pdb-into-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!