【问题标题】:Can I use VS2010's Intellitrace to gather data for a Windows Service?我可以使用 VS2010 的 Intellitrace 为 Windows 服务收集数据吗?
【发布时间】:2010-05-26 17:15:29
【问题描述】:

我有一个 Windows 服务,我想收集一些有关使用 Intellitrace 的调试数据 - 问题是您无法通过直接从 VS 内部启动它来调试 Windows 服务。我已经安装了服务,Service.Start 中的第一条语句是“Debug.Break”,它允许我附加 VS。但是,如果在附加时进程已经启动,则不能使用 Intellitrace。

有人知道解决方法吗?

【问题讨论】:

    标签: .net debugging visual-studio-2010 intellitrace windows-services


    【解决方案1】:

    只需一点点工作即可。总体思路是模拟一个将调用服务的 OnStart 和 OnStop 方法的控制台应用程序。这不是服务将经过的确切开始和停止路径,但希望它能让您达到可以诊断问题的地步。我包含了一些示例代码来给你一个大致的想法。

    ConsoleMock.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using WindowsService1;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Service1 s1 = new Service1();
                while (true)
                {
                    Console.WriteLine(">1 Start\n>2 Stop");
                    string result = Console.ReadLine();
                    if (result == "1")
                    {
                        var method = s1.GetType().GetMethod("OnStart", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        method.Invoke(s1, new object[] { args });
                    }
                    else if (result == "2")
                    {
                        var method = s1.GetType().GetMethod("OnStop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        method.Invoke(s1, new object[] { });
                    }
                    else
                    {
                        Console.WriteLine("wrong command");
                    }
                }
            }
        }
    }
    
    
    Service.cs:
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Diagnostics;
        using System.Linq;
        using System.ServiceProcess;
        using System.Text;
        using System.Threading;
    
        namespace WindowsService1
        {
            public partial class Service1 : ServiceBase
            {
                private long serviceCounter;
                private Thread workerThread;
    
                public Service1()
                {
                    InitializeComponent();
                    serviceCounter = 0;
    
                }
    
                public void Worker()
                {
                    while (true)
                    {
                        serviceCounter += 1;
                        System.Threading.Thread.Sleep(500);
    
                        try
                        {
                            throw new Exception(serviceCounter.ToString());
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
    
                protected override void OnStart(string[] args)
                {
                    workerThread = new Thread(new ThreadStart(Worker));
                    workerThread.Start();
                }
    
                protected override void OnStop()
                {
                    workerThread.Abort();
                }
            }
        }
    

    【讨论】:

    • 这简直不能再像冠军了!实际上,我已经使用它创建了一个存根应用程序来测试我的 Windows 服务,而无需手动附加调试器——我的 WinForm 只有一个使用相同代码的启动和停止按钮,并且它运行良好。如果可以的话,我给你三票!
    【解决方案2】:

    Evan 的技术没有回答最初的问题。

    有关解决方案,请参阅http://blogs.msdn.com/b/msaffer/archive/2011/02/23/using-intellitrace-with-services.aspx。马上在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services*myservice*下用三行创建一个注册表多字符串值名称“Environment”

    COR_ENABLE_PROFILING=1
    COR_PROFILER={301EC75B-AD5A-459C-A4C4-911C878FA196}
    VSLOGGERCPLAN=Put_here_the_path_to_your_CollectionPlan.xml
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2015-06-29
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多