{"id":27874684,"url":"https://github.com/ctripcorp/cat.net","last_synced_at":"2025-09-06T22:31:19.063Z","repository":{"id":33651481,"uuid":"37303941","full_name":"ctripcorp/cat.net","owner":"ctripcorp","description":".Net Client of CAT","archived":false,"fork":false,"pushed_at":"2016-04-21T06:32:56.000Z","size":2740,"stargazers_count":68,"open_issues_count":1,"forks_count":46,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-05T01:39:46.100Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ctripcorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-12T05:33:26.000Z","updated_at":"2025-03-28T09:47:17.000Z","dependencies_parsed_at":"2022-08-17T19:05:46.072Z","dependency_job_id":null,"html_url":"https://github.com/ctripcorp/cat.net","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ctripcorp/cat.net","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fcat.net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fcat.net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fcat.net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fcat.net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ctripcorp","download_url":"https://codeload.github.com/ctripcorp/cat.net/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fcat.net/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273973413,"owners_count":25200575,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-05T01:37:22.228Z","updated_at":"2025-09-06T22:31:19.040Z","avatar_url":"https://github.com/ctripcorp.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CAT.net客户端 [![Build Status](https://travis-ci.org/ctripcorp/cat.net.png?branch=master)](https://travis-ci.org/ctripcorp/cat.net)\n为.net应用提供接入CAT的API。\n\nCAT.net客户端的API设计、客户端配置方式，与[CAT Java客户端](https://github.com/dianping/cat)基本一致。\n\n### 编译工程\nCAT.net客户端要求**_.NET Framework 4.0或更高版本_**。\n\n用Visual Studio 2010或更高版本，打开Cat\\Cat.sln。可以看到Solution中包括两个工程：\n\n1. `Cat`：CAT.net客户端实现代码\n2. `CatClientTest`：示例程序和测试用例。\n\n单击Rebuild Solution编译这两个工程：\n![Rebuild Solution](doc/rebuild-solution.png)\n\n编译的输出是`Cat.dll`，如下图。在业务应用的工程中，通过引用这个dll，调用其中的API，来接入CAT。\n![Cat DLL](doc/cat-dll-location.png)\n\n### 配置\n1. 创建以下目录，确保执行CAT客户端的帐户有权限读写它们：\n  - `d:\\data\\appdatas\\cat\\`  (CAT客户端使用的临时数据目录)\n  - `d:\\data\\applogs\\cat\\`  (CAT客户端的日志输出目录)\n2. 创建`d:\\data\\appdatas\\cat\\client.xml`。在其中配置Domain ID和CAT服务器地址。推荐client.xml用**_UTF-8_**编码。client.xml内容如下：\n```\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cconfig mode=\"client\" enabled=\"true\" queue-size=\"123\"\u003e\n\t\u003c!--logEnabled enabled=\"true\"\u003e\u003c/logEnabled--\u003e\n\t\n\t\u003c!-- 配置Domain ID--\u003e\n\t\u003cdomain id=\"1237\" enabled=\"true\" max-message-size=\"1000\"/\u003e\n\t\n\t\u003cservers\u003e\n\t\t\u003c!-- 配置CAT服务器地址--\u003e\n\t\t\u003cserver ip=\"10.2.6.98\" port=\"2280\" http-port=\"8080\"\u003e\u003c/server\u003e\n\t\u003c/servers\u003e\n\u003c/config\u003e\n```\n\n### 执行工程自带的测试用例\n- 设置CatClientTest工程为默认启动工程：\n\n![Set CatClientTest to be Startup Project](doc/set-startup-project.png)\n\n- 单击执行，就会运行`CatClientTest`中的`Program.cs`的`Main()`方法。\n![Run CatClientTest](doc/run-cat-client-test.png)\n\n程序输出：\n\n![CatClientTest output](doc/catclienttest-output.png)\n\n- 在CAT中可以看到测试程序的CAT埋点，如下图。其中的CAT服务器地址、Domain ID应该与client.xml中的配置一致。\n![CatClientTest Transactions](doc/catclienttest-transactions.png)\n\n### 在其他应用中引用`Cat.dll`，调用CAT API\n- 假设我们有一个console应用。\n![Create New Console Project](doc/new-console-project.png)\n确保工程使用了.NET Framework 4.0或更高版本的服务端Profile，而**_不是Client Profile_**。\n![Use Server Profile](doc/use-server-profile.png)\n- 添加对`Cat.dll`的引用\n![Add Reference](doc/add-reference.png)\n- 调用CAT API埋点。示例代码：\n```\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing Org.Unidal.Cat.Message;\nusing Org.Unidal.Cat;\nusing System.Threading;\n\nnamespace ConsoleApplication1\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            ITransaction transaction = null; ;\n            try\n            {\n                transaction = Cat.NewTransaction(\"Order\", \"Cash\");\n\n                // Do your business...\n\n                Cat.LogEvent(\"City\", \"Shanghai\");\n                transaction.Status = CatConstants.SUCCESS;\n            }\n            catch (Exception ex)\n            {\n            \tCat.LogError(ex);\n                transaction.SetStatus(ex);\n                \n                // You may need to re-throw exception ex out.\n            }\n            finally\n            {\n                transaction.Complete();\n\n                // 程序退出前睡一会儿。使得CAT客户端有时间发出最后一批消息到网络。\n                Thread.Sleep(1000);\n            }\n        }\n    }\n}\n```\n- 执行以上`Main()`方法。\n- 在CAT中可以看到埋点效果：\n![Application Output](doc/console-app-output.png)\n\n![Application Output Logview](doc/console-app-output-logview.png)\n\n### 日志输出\n- 在client.xml中，启用`\u003clogEnabled enabled=\"true\"\u003e\u003c/logEnabled\u003e` XML元素，以开启日志输出。\n- 日志输出位于`D:\\data\\applogs\\cat`目录中：\n![Application Log File Path](doc/console-app-log-file-path.png)\n\n### 为心跳报表获取.NET性能数据\n![Heartbeat Report](doc/heartbeat-output.png)\n如上图，CAT.net客户端每分钟会自动抓取一次机器性能数据，展现在Heartbeat报表中，包括CPU利用率、GC次数、Heap各代大小、锁竞争次数、锁请求队列大小等。\n\n这些性能指标中的一部分，是通过读取.NET Performance Counter实现的。\n（见`DefaultPerformanceMetricProvider.cs`中的`Initialize()`，如下图）。\n![Default Performance Metric Provider](doc/default-performance-metric-provider.png)\n\n然而，读取.NET Performance Counter，需要执行应用所使用的帐户是**_Performance Log Users_**用户组的成员（参考这里）。特别地，在IIS中以普通账户（`IIS_USRS`）执行的Web应用，是没有这个权限的。\n\n所以，您可能需要提供自己的`IPerformanceMetricProvider`实现，它通过其他有权限读取的数据源（如Zabbix Agent，Salt Agent，或自己实现的一个有更高执行权限的Performance Counter输出程序）来获取这些性能指标。\n\t\n在您的实现中，对于`IPerformanceMetricProvider`接口中`的Get*()`方法，它应当返回**过去1分钟内**的某个性能指标的累积值（如GC次数），或**过去1分钟内**的平均值（如CPU利用率）。 不推荐它返回从**程序启动至今**的累积值/平均值。请参考`DefaultPerformanceMetricProvider`是如何遵循这一语义的。\n\n通过修改`StatusUpdateTask.cs`，用您的`IPerformanceMetricProvider`实现，来替换掉`DefaultPerformanceMetricProvider`这个默认实现，如下图。\n![Instantiate IPerformance Metric Provider](doc/instantiate-performance-metric-provider.png)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctripcorp%2Fcat.net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctripcorp%2Fcat.net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctripcorp%2Fcat.net/lists"}