1. 新建 .NET6 Web程序集
名称: MySkywalking.Web
1.1 引用 SkyAPM.Agent.AspNetCore
2.修改 Program.cs
2.1 增加环境变量配置
2.2 添加 Skywalking服务
builder.Services.AddSkyAPM();
2.3 program.cs 参照代码如下:
namespace MySkywalking.Web
{
public class Program
{
public static void Main(string[] args)
{
// Skywalking 必须配置,HostingStartup 增强启动
Environment.SetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", "SkyAPM.Agent.AspNetCore");
//必须配置,在skywalking做标识,服务名称
Environment.SetEnvironmentVariable("SKYWALKING__SERVICENAME", "MySkywalking8");
System.Threading.ThreadPool.SetMinThreads(1024, 512);
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSkyAPM();//添加Skywalking相关配置
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
}
}
}
3.根目录添加 skyapm.json配置
文件的属性”复制到输出目录“ 修改为 ”如果较新则复制”
{
"SkyWalking": {
"ServiceName": "MySkywalking8", //服务名称,同 Program.cs中 SKYWALKING__SERVICENAME 一致
"Namespace": "",
"HeaderVersions": [
"sw8"
],
"Sampling": {
"SamplePer3Secs": -1,
"Percentage": -1.0
},
"Logging": {
"Level": "Error",
"FilePath": "logs\\skyapm-{Date}.log"
},
"Transport": {
"Interval": 3000,
"ProtocolVersion": "v8",
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "Skywalking服务器IP:11800", //skywalking 服务器Ip,端口11800为数据写入端口
"Timeout": 10000,
"ConnectTimeout": 10000,
"ReportTimeout": 600000,
"Authentication": ""
}
}
}
}
4. 运行Web站点如下图:
5. 打开Skywalking UI,查看Skywaling 追踪信息
6. .NET6 接入Skywalking Success.
附录:
官方参照文档: https://github.com/SkyAPM/SkyAPM-dotnet
Server Agent:https://skywalking.apache.org/docs/main/next/en/setup/service-agent/server-agents/