live-forum/server/webapi/LiveForum/LiveForum.Code/Extend/UseAppDefaultRequest.cs
2026-03-24 11:27:37 +08:00

45 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Builder;
using System.Diagnostics;
using System.Reflection;
namespace LiveForum.Code.Extend;
/// <summary>
///
/// </summary>
public static class UseAppDefaultRequest
{
/// <summary>
/// 添加默认请求
/// </summary>
/// <param name="app"></param>
public static void UseAppRequest(this WebApplication app, string name = "")
{
app.MapGet("/", () => $"请求成功=》{name}==>{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}").WithName("默认请求");
var startDateTime = DateTime.Now;
var InformationalVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
Console.WriteLine($"version:{InformationalVersion}");
app.MapGet("/system", () =>
{
using Process currentProcess = Process.GetCurrentProcess();
// CPU使用率 (一般是一个0-100之间的值但实际是时间占比需要转换)
double cpuUsage = currentProcess.TotalProcessorTime.TotalMilliseconds / Environment.TickCount * 100;
// 已用内存 (字节)
long memoryUsage = currentProcess.WorkingSet64;
return new
{
msg = $"系统信息:启动时间:{startDateTime.ToString("yyyy-MM-dd HH:mm:ss")},已安全运行时间:{DateTime.Now.Subtract(startDateTime).TotalMinutes.ToString("0.##")}分钟",
startDateTime,
MemoryUsage = $"{memoryUsage / (1024.0 * 1024.0):F2}MB",
CPUUsage = $"{cpuUsage:F2}%"
};
}).WithName("获取系统数据");
}
}