// 自定义性能计数器类(带注释)
public class CustomPerformanceCounter
{
private readonly PerformanceCounter _requestsPerSecond;
private readonly PerformanceCounter _memoryUsage;
public CustomPerformanceCounter()
{
// 创建请求计数器
_requestsPerSecond = new PerformanceCounter("Custom Category", "Requests Per Second", "Instance1", true);
_requestsPerSecond.RawValue = 0;
// 创建内存使用率计数器
_memoryUsage = new PerformanceCounter("Memory", "Available MBytes");
}
public void IncrementRequests()
{
_requestsPerSecond.Increment();
}
public double GetMemoryUsage()
{
return _memoryUsage.NextValue();
}
}
// 使用ClrMD进行内存转储分析(需安装ClrMD NuGet包)
using Microsoft.Diagnostics.Runtime;
public class MemoryDumper
{
public void DumpHeap(string processName)
{
using var dataTarget = DataTarget.CreateProcess(processName);
using var clrInfo = dataTarget.ClrVersions[0];
using var heap = clrInfo.GetHeap();
// 遍历所有对象
foreach (var obj in heap.EnumerateObjects())
{
if (obj.Type.Name == "ProblematicClass")
{
Console.WriteLine($"Found {obj.Type.Name} at {obj.Address}");
}
}
}
}
// 使用结构体避免大对象堆压力
public struct LargeDataStructure
{
public byte[] Data;
public int Length;
}
// 避免频繁创建大对象
public class DataProcessor
{
private readonly LargeDataStructure _buffer = new LargeDataStructure { Data = new byte[1024 * 1024] };
public void ProcessData()
{
// 复用缓冲区
_buffer.Data[0] = 0xFF;
}
}
// 自适应线程池(带注释)
public class AdaptiveThreadPool
{
private readonly BlockingCollection<Action> _tasks = new BlockingCollection<Action>();
private readonly int _minThreads = Environment.ProcessorCount * 2;
private readonly int _maxThreads = Environment.ProcessorCount * 4;
public AdaptiveThreadPool()
{
for (int i = 0; i < _minThreads; i++)
{
Task.Run(() => Worker());
}
}
private void Worker()
{
foreach (var task in _tasks.GetConsumingEnumerable())
{
try
{
task();
}
catch (Exception ex)
{
// 异常处理
}
}
}
public void QueueTask(Action task)
{
_tasks.Add(task);
// 动态扩展线程数
if (_tasks.Count > _maxThreads * 2)
{
Task.Run(() => Worker());
}
}
}
// 使用Channel实现高效异步管道(C# 8.0+)
public class AsyncPipeline
{
private readonly Channel<string> _inputChannel = Channel.CreateUnbounded<string>();
private readonly Channel<string> _outputChannel = Channel.CreateUnbounded<string>();
public async Task ProcessAsync()
{
await Task.WhenAll(
ProcessStage1(),
ProcessStage2(),
ProcessStage3()
);
}
private async Task ProcessStage1()
{
await foreach (var item in _inputChannel.Reader.ReadAllAsync())
{
// 处理逻辑
await _outputChannel.Writer.WriteAsync(item);
}
}
// 其他处理阶段...
}
// 使用EF Core的Include优化(带注释)
public class EfQueryOptimizer
{
public IQueryable<Order> GetOrdersWithDetails()
{
return dbContext.Orders
.Include(o => o.Customer)
.Include(o => o.Items)
// 避免N+1查询
.ThenInclude(i => i.Product)
.AsNoTracking() // 禁用跟踪
.Where(o => o.Status == OrderStatus.Active);
}
}
// 使用ExecuteSqlRaw优化复杂查询
public class RawQueryExample
{
public async Task<List<Order>> GetOrdersByDate(DateTime date)
{
var sql = @"
SELECT * FROM Orders
WHERE OrderDate >= @date
AND Status = @status
OPTION (RECOMPILE)";
return await dbContext.Orders
.FromSqlRaw(sql, new SqlParameter("@date", date), new SqlParameter("@status", (int)OrderStatus.Active))
.ToListAsync();
}
}
// 使用Redis事务保证缓存一致性
public class RedisCacheManager
{
private readonly IDatabase _cache;
public async Task SetCacheWithLock(string key, object value, TimeSpan expiry)
{
var lockKey = $"lock:{key}";
var lockToken = Guid.NewGuid().ToString();
// 尝试获取分布式锁
if (await _cache.StringSetAsync(lockKey, lockToken, TimeSpan.FromSeconds(10), When.NotExists))
{
try
{
// 执行缓存操作
await _cache.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);
}
finally
{
// 释放锁
if (await _cache.StringGetAsync(lockKey) == lockToken)
{
await _cache.KeyDeleteAsync(lockKey);
}
}
}
}
}
// 使用QuicStream实现低延迟通信
public class QuicClient
{
private readonly QuicConnection _connection;
public async Task SendDataAsync(byte[] data)
{
using var stream = await _connection.OpenUnidirectionalStreamAsync();
await stream.WriteAsync(data);
}
public async Task<byte[]> ReceiveDataAsync()
{
using var stream = await _connection.AcceptUnidirectionalStreamAsync();
var buffer = new byte[4096];
await stream.ReadAsync(buffer);
return buffer;
}
}
// 使用 Polly 实现指数退避重试
public class RetryPolicy
{
private readonly AsyncPolicy _policy;
public RetryPolicy()
{
_policy = Policy
.Handle<HttpRequestException>()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: retry => TimeSpan.FromSeconds(Math.Pow(2, retry)),
onRetry: (ex, time) => Console.WriteLine($"Retrying in {time.TotalSeconds}s: {ex.Message}")
);
}
public async Task ExecuteAsync(Func<Task> action)
{
await _policy.ExecuteAsync(action);
}
}
// 使用Prometheus.NET实现指标暴露
public class MetricsMiddleware
{
private readonly RequestDelegate _next;
private readonly MetricsService _metrics;
public MetricsMiddleware(RequestDelegate next, MetricsService metrics)
{
_next = next;
_metrics = metrics;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path == "/metrics")
{
await _metrics.WriteTextAsync(context.Response.Body);
return;
}
await _next(context);
}
}
// 在Startup.cs中注册
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<MetricsMiddleware>();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
# Deployment配置示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: myapp
image: myapp:latest
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
// 使用 SOS 调试扩展分析内存转储
!dumpheap -stat
!gcroot 00007FFB2A3C0000
!clrstack
// 通过Thread.Dump获取线程状态
public class ThreadDumper
{
public void DumpAllThreads()
{
foreach (var thread in Process.GetCurrentProcess().Threads)
{
Console.WriteLine($"Thread ID: {thread.Id}");
Console.WriteLine($"Priority: {thread.PriorityLevel}");
Console.WriteLine($"BasePriority: {thread.BasePriority}");
Console.WriteLine($"WaitReason: {thread.WaitReason}");
}
}
}
现象:GC事件频繁,吞吐量下降
解决方案:
// 启用并发GC
<runtime>
<gcConcurrent enabled="true"/>
<gcServer enabled="true"/>
</runtime>
现象:ThreadPool.QueueUserWorkItem抛出InvalidOperationException
解决方案:
// 扩展线程池容量
ThreadPool.SetMaxThreads(Environment.ProcessorCount * 4, Environment.ProcessorCount * 4);
现象:System.Data.SqlClient.SqlException: Timeout expired
解决方案:
// 配置连接池
<connectionStrings>
<add name="MyDb" connectionString="Data Source=.;Max Pool Size=200;Min Pool Size=20;" />
</connectionStrings>
本文展示了如何构建一个覆盖内存、线程、网络、数据库的全方位监控与优化体系。某证券系统通过本方案实现:
通过这套架构,C#应用在生产环境中真正实现了"监控即开发,优化即运维"。