我可以使用 VS2010 的 Intellitrace 来收集 Windows 服务的数据吗?

发布于 2024-09-03 06:11:06 字数 203 浏览 5 评论 0原文

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

有人知道这个问题的解决方法吗?

I have a Windows service that I'd like to gather some debugging data on using Intellitrace - the problem is that you can't debug a Windows Service by starting it directly from inside VS. I have the service installed, and the very first statement in Service.Start is "Debug.Break", which allows me to attach VS. However, you can't use Intellitrace if a process is already started when you attach.

Does anybody know of a workaround for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-09-10 06:11:06

只需一点点工作就有可能实现。总体思路是模拟一个控制台应用程序,该应用程序将调用服务的 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();
            }
        }
    }

It is possible with a little bit of work. The general idea is to mock up a console application that will call the OnStart and OnStop methods of the service. It's not the exact start and stop path a service will go through but hopefully it get you to the point that you can diagnose your issue. I included some sample code to give you a general idea.

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();
            }
        }
    }
洋洋洒洒 2024-09-10 06:11:06

埃文的技术并没有回答最初的问题。

有关解决方案,请参阅 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

Evan's technique does not answer the original question.

For a solution see http://blogs.msdn.com/b/msaffer/archive/2011/02/23/using-intellitrace-with-services.aspx. Shortly create a registry multi-string value name "Environment" under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services*myservice* with three lines

COR_ENABLE_PROFILING=1
COR_PROFILER={301EC75B-AD5A-459C-A4C4-911C878FA196}
VSLOGGERCPLAN=Put_here_the_path_to_your_CollectionPlan.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文