Take a look at GW

【New Relic】给基于.NET Framework 4.0 的 exe 程序安装New Relic

由于.NET Framework 4.0 是比较古老的版本,只有New Relic 7.0以下的版本才会支持.NET Framework 4.0的引用程序。 Technical support for .NET Framework 4.0 or lower

 

你可以参考这个官方Install New Relic to Monitor your App的文档。

 

1. 创建New Relic 账号

创建一个New Relic 账号,并且获取你的New Relic License key,由于我的程序不是托管到IIS的web程序,所以使用Ingest-License类型的Key.

 

你可以参考官方的 New Relic API keys 或是直接访问 API keys UI page . 来查看你自己的License Key.

 

2. 安装new relic agent

通过这个页面 https://download.newrelic.com/dot_net_agent/6.x_release/ 下载win-x64-6.27.0.0的版本。

这里我下载的是newrelic-agent-win-6.27.0.0-scriptable-installer.zip 并解压缩

用管理员权限打开命令行窗口工具,执行下面命令

install.cmd -LicenseKey your_license_key -InstrumentAll

将 your_license_key 替换为你自己的 license key。 由于默认安装的New Relic Agent是不监听非IIS程序的,所以要加上 -InstrumentAll 让Agent能够监听所有程序。

 

安装完成后,你的电脑会多出来两个Agent的目录,C:\ProgramData\New Relic 和 C:\Program Files\New Relic文件夹。

 

3. 设置 agent目录权限

在C:\ProgramData\New Relic下面,给.Net Agent目录加上所有人权限。

 

4. 更改new relic配值

用记事本打开本地的C:\ProgramData\New Relic\.NET Agent\newrelic.config配置文件。

 

观察service上的licenseKey是否正确,并且增加 sendDataOnExit="true" sendDataOnExitThreshold="1000"的配置。

在application的name标签下指定程序的名称,这个名称后面会被推送到NewRelic的Dashboard上。

在Instruction 的application标签下指定要运行的EXE。

  <service licenseKey="your_license_key" ssl="true" sendDataOnExit="true" sendDataOnExitThreshold="1000"/>
  <application>
    <name>Your_Application_Name</name>
  </application>

  <instrumentation>
    <applications>
      <application name="your_exe_folder\your_application.exe" />
    </applications>
  </instrumentation>

下面是我本地的New Relic 配置

<?xml version="1.0"?>
<!-- Copyright (c) 2008-2017 New Relic, Inc.  All rights reserved. -->
<!-- For more information see: https://newrelic.com/docs/dotnet/dotnet-agent-configuration -->
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
  <service licenseKey="your_license_key" ssl="true" sendDataOnExit="true" sendDataOnExitThreshold="1000"/>
  <application>
    <name>AudaParts Job7</name>
  </application>

  <instrumentation>
    <applications>
      <application name="your_exe_folder\your_application.exe" />
    </applications>
  </instrumentation>

  <log level="info" />
  <transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="false" explainThreshold="500" />
  <crossApplicationTracer enabled="true" />
  <errorCollector enabled="true">
    <ignoreErrors>
      <exception>System.IO.FileNotFoundException</exception>
      <exception>System.Threading.ThreadAbortException</exception>
    </ignoreErrors>
    <ignoreStatusCodes>
      <code>401</code>
      <code>404</code>
    </ignoreStatusCodes>
  </errorCollector>
  <browserMonitoring autoInstrument="true" />
  <threadProfiling>
    <ignoreMethod>System.Threading.WaitHandle:InternalWaitOne</ignoreMethod>
    <ignoreMethod>System.Threading.WaitHandle:WaitAny</ignoreMethod>
  </threadProfiling>
</configuration>
View Code

记得将liceseKey,name 和 Application name替换为你自己的数据。

 

5. 添加Extentions

由于我们想要New Relic 监控非IIS的托管的程序,在上面的配置中,我们已经指定要监控的exe程序 your_exe_folder\your_application.exe 。现在我们需要告诉New Relic 程序需要监控EXE中的那些数据,这就是Extension的作用。Extension都是xml文件,并且放置在C:\ProgramData\New Relic\.NET Agent\Extensions目录下,我们只需要将编写好的Extension放到该目录下即可。

 

比如我们EXE的代码中有如下的函数需要监控

namespace Order.Core.Business
{
  public partial class OrderManager
  {
    public void GenerateOrdersAutomatically(UserManager objUser, bool quotationAuction = false)
    {
        //some code in here
    }
  }
}

 

如果上面这段程序编译出来的代码dll的程序集名称是 Order.Core 的话,那么只需要参考官方文档给的格式 Create transactions via XML (.NET) 填写上数据就可以了

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
  <instrumentation>
    <!-- Define the method which triggers the creation of a transaction. -->
    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="TransactionName">
      <match assemblyName="AssemblyName" className="NameSpace.ClassName">
        <exactMethodMatcher methodName="MethodName" />
      </match>
    </tracerFactory>
    <!-- Define the method which triggers the creation of a transaction. -->
    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="TransactionName2">
      <match assemblyName="AssemblyName" className="NameSpace.ClassName2">
        <exactMethodMatcher methodName="MethodName2" />
      </match>
    </tracerFactory>
    <!-- Define the method which triggers the creation of a transaction. -->
    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="TransactionName3">
      <match assemblyName="AssemblyName" className="NameSpace.ClassName3" minVersion="1.0.0" maxVersion="99.99.99">
        <exactMethodMatcher methodName="MethodName3" />
      </match>
    </tracerFactory>
  </instrumentation>
</extension>

 

那么我的案例的extension xml就应该是如下的格式

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2020 New Relic Corporation. All rights reserved.
SPDX-License-Identifier: Apache-2.0 -->
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory"
                       metricName="GenerateOrdersAutomatically">
            <match assemblyName="Order.Core"
                   className="Order.Core.Business.OrderManager">
                <exactMethodMatcher methodName="GenerateOrdersAutomatically" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

 

注意,extension 不需要给每个方法都配置,一般只需要按照Transaction分类后的,配置每个Transaction的入口方法就可以了。New Relic会自动追钟每个函数下面调用的其它函数和数据库(如果有调用的数据库的话)。没有必要给Console 程序 Main方法配置Extension,因为Main方法太宽泛了,如果将Main配置成一个单独的Transaction, 那么这个Transaction下面就会囊括所有的方法调用,这样就失去了Transaction分类监控的意义了。

 

 

昨晚上面这些配置的话,New Relic的监控就应该已经启动了。

 

6. 观察日志

在C:\ProgramData\New Relic\.NET Agent\Logs目录下面,有New Relic的所有运行日志。如果没有问题的话,你应该可以看到Metric harvest starting, Reporting to: 这些信息。

 

 

你可以直接点开Reporting to后面的连接,会跳转到New Relic web网页上,上面就可以看到详细的信息。

 

7. 观察New Relic Dashboard

打开New Relic Web,将 Transaction Type 选成  Non-Web  就可以查看我们监控的exe的数据啦

 

从一个Transaction点进去,你可以看到整个调用流程,调用每个函数花费的时间,和数据库的SQL和花费时间等等。还可以观察错误日志。

 

有时,Agent 上传数据到New Relic Web 有时候会有延迟。

 

如果你遇到了TLS的问题导致无法连接到collector.newrelic.com的话

Unable to connect to the New Relic service at collector.newrelic.com:443

可以参考No data appears after disabling TLS 1.0 | New Relic Documentation的解决方案。

posted @ 2024-06-18 16:01  HDWK  阅读(63)  评论(0编辑  收藏  举报