1
0

GUI 啟動完成

This commit is contained in:
2024-03-25 17:39:44 +08:00
parent 739e92cee8
commit 3382627c8c
18 changed files with 299 additions and 48 deletions

View File

@@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coreprofiler" Version="1.1.4" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,39 @@
using CoreProfiler;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace CoreProfilerExample.Common.Extensions
{
public static class MethodBaseExtension
{
public static IDisposable ProfilingStep(this MethodBase method, string? stepName = null)
{
if (stepName == null)
{
// 取得方法的類型物件
var methodType = method.ReflectedType;
// 取得類別的類型物件
var classType = methodType?.ReflectedType ?? methodType;
// 取出類別底下所有非同步類型的方法,比對出當前的方法類型
var classMethodType = classType?
.GetMethods()
.Select(x => new
{
Method = x,
Attribute = x.GetCustomAttribute<AsyncStateMachineAttribute>(),
})
.FirstOrDefault(x => x.Attribute?.StateMachineType == methodType);
var className = classType?.Name ?? "無法取得類別名稱";
var methodName = classMethodType?.Method.Name ?? method.Name ?? "無法取得方法名稱";
stepName = $"{classType?.Name}.{methodName}";
}
return ProfilingSession.Current.Step(stepName);
}
}
}

View File

@@ -0,0 +1,61 @@
using Newtonsoft.Json;
namespace CoreProfilerExample.Common.Options
{
public class CoreProfilerOption
{
/// <summary>
/// 資料保存上限(Session)。
/// </summary>
[JsonProperty("circularBufferSize")]
public int CircularBufferSize { get; set; } = 200;
/// <summary>
/// 要過濾掉的項目。
/// </summary>
[JsonProperty("filters")]
public IEnumerable<CoreProfilerOptionFilter> Filters { get; set; } = [
new CoreProfilerOptionFilter
{
Key = "/coreprofiler",
Value = "/coreprofiler",
Type = "CoreProfiler.ProfilingFilters.NameContainsProfilingFilter, CoreProfiler",
},
new CoreProfilerOptionFilter
{
Key = "static files",
Value = "ico,jpg,js,css,svg,json,ttf,woff,woff2,eot",
Type = "CoreProfiler.ProfilingFilters.FileExtensionProfilingFilter, CoreProfiler",
},
];
public CoreProfilerOption Save(string filename)
{
var json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(filename, json);
return this;
}
public static CoreProfilerOption UseCoreProfilerSetting()
{
var filename = Path.Combine(AppContext.BaseDirectory, "coreprofiler.json");
if (File.Exists(filename))
{
try
{
var json = File.ReadAllText(filename);
return JsonConvert.DeserializeObject<CoreProfilerOption>(json);
}
catch
{
}
}
return new CoreProfilerOption().Save(filename);
}
}
}

View File

@@ -0,0 +1,14 @@
using Newtonsoft.Json;
namespace CoreProfilerExample.Common.Options
{
public class CoreProfilerOptionFilter
{
[JsonProperty("key")]
public string Key { get; set; } = null!;
[JsonProperty("value")]
public string Value { get; set; } = null!;
[JsonProperty("type")]
public string Type { get; set; } = null!;
}
}