Files
xSaherelmWorkspace/Documents/Docs/Developer/Adding OpenTelemetry, Sentry and Handle Logging Requests 14050817.html

1003 lines
35 KiB
HTML
Raw Permalink 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.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>گزارش فنی: ارتقای xLoggingService با OpenTelemetry، میدل‌ور و Sentry</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge {
display: flex;
align-items: center;
gap: 12px;
}
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta {
text-align: left;
font-size: 12px;
opacity: 0.9;
}
.doc-meta div { margin-bottom: 3px; }
.document-title {
text-align: center;
position: relative;
z-index: 1;
padding: 20px 0;
}
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 {
font-size: 16px;
font-weight: 400;
opacity: 0.9;
}
.document-body {
padding: 40px;
}
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section {
margin-bottom: 40px;
page-break-inside: avoid;
}
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 {
font-size: 19px;
font-weight: 700;
color: var(--primary);
margin-bottom: 2px;
}
.section-title .subtitle {
font-size: 12px;
color: var(--text-muted);
}
.section-content {
padding-right: 10px;
}
.section-content p {
margin-bottom: 12px;
text-align: justify;
text-indent: 20px;
}
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong {
color: var(--accent-dark);
}
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th {
padding: 14px 16px;
text-align: right;
font-weight: 600;
font-size: 13px;
}
.styled-table td {
padding: 13px 16px;
border-bottom: 1px solid var(--border);
font-size: 13px;
}
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row {
background: #fffaf0 !important;
font-weight: 600;
}
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 25px 0;
}
.comparison-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 20px;
position: relative;
}
.comparison-card.recommended {
border-color: var(--success);
background: linear-gradient(to left, #f0fff4, #e6fffa);
}
.comparison-card .card-badge {
position: absolute;
top: -10px;
right: 20px;
background: var(--success);
color: white;
padding: 3px 12px;
border-radius: 15px;
font-size: 11px;
font-weight: 600;
}
.comparison-card h4 {
font-size: 16px;
font-weight: 700;
color: var(--primary);
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid var(--border);
}
.comparison-card.recommended h4 {
border-bottom-color: var(--success);
}
.comparison-card ul {
list-style: none;
padding: 0;
}
.comparison-card ul li {
padding: 8px 0;
border-bottom: 1px solid var(--border);
font-size: 13px;
display: flex;
justify-content: space-between;
}
.comparison-card ul li:last-child { border-bottom: none; }
.comparison-card ul li .value {
font-weight: 600;
color: var(--primary);
}
.quote-box {
background: linear-gradient(135deg, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin: 20px 0;
position: relative;
}
.quote-box::before {
content: '"';
position: absolute;
top: -10px;
right: 15px;
font-size: 60px;
color: var(--accent);
opacity: 0.3;
font-family: Georgia, serif;
}
.quote-box p {
font-style: italic;
color: var(--text-muted);
margin-bottom: 10px;
text-indent: 0 !important;
}
.quote-box .source {
font-size: 12px;
color: var(--text-muted);
text-align: left;
font-style: normal;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin: 25px 0;
}
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value {
font-size: 24px;
font-weight: 800;
margin-bottom: 5px;
color: var(--accent);
line-height: 1.3;
}
.stat-card .stat-label {
font-size: 12px;
opacity: 0.9;
}
.timeline {
position: relative;
padding: 20px 0;
margin: 25px 0;
}
.timeline::before {
content: '';
position: absolute;
right: 50%;
top: 0;
bottom: 0;
width: 3px;
background: var(--border);
transform: translateX(50%);
}
.timeline-item {
display: flex;
margin-bottom: 20px;
position: relative;
}
.timeline-item:nth-child(odd) {
flex-direction: row-reverse;
}
.timeline-content {
width: 45%;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
padding: 15px;
position: relative;
}
.timeline-item:nth-child(odd) .timeline-content {
margin-left: 5%;
}
.timeline-item:nth-child(even) .timeline-content {
margin-right: 5%;
}
.timeline-dot {
position: absolute;
right: 50%;
top: 20px;
width: 16px;
height: 16px;
background: var(--accent);
border: 3px solid white;
border-radius: 50%;
transform: translateX(50%);
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
z-index: 1;
}
.timeline-content h5 {
font-size: 14px;
font-weight: 700;
color: var(--primary);
margin-bottom: 8px;
}
.timeline-content p {
font-size: 12px;
color: var(--text-muted);
text-indent: 0 !important;
margin-bottom: 0 !important;
}
.reference-list {
list-style: none;
padding: 0;
margin: 20px 0;
}
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong {
color: var(--primary);
display: block;
margin-bottom: 5px;
}
.reference-list li .ref-source {
font-size: 11px;
color: var(--text-muted);
margin-top: 5px;
}
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 {
font-size: 22px;
font-weight: 800;
margin-bottom: 15px;
color: var(--accent);
}
.conclusion-box p {
font-size: 14px;
line-height: 1.8;
text-indent: 0 !important;
margin-bottom: 15px;
}
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
.footer-divider {
width: 60px;
height: 2px;
background: var(--accent);
margin: 10px auto;
}
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
pre {
background: #1a202c;
color: #e2e8f0;
padding: 18px;
border-radius: 8px;
overflow-x: auto;
font-size: 13px;
line-height: 1.6;
direction: ltr;
text-align: left;
margin: 15px 0;
}
code {
font-family: 'Consolas', 'Monaco', monospace;
background: #edf2f7;
padding: 2px 6px;
border-radius: 4px;
font-size: 13px;
color: var(--primary);
}
pre code {
background: transparent;
padding: 0;
color: inherit;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.comparison-grid { grid-template-columns: 1fr; }
.stats-grid { grid-template-columns: 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: XLOG-OBS-1405-001</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: فنی</div>
</div>
</div>
<div class="document-title">
<span class="label">TECHNICAL REPORT</span>
<h1>ارتقای xLoggingService با OpenTelemetry، میدل‌ور و Sentry</h1>
<h2>افزودن قابلیت رهگیری توزیع‌شده، لاگ درخواست‌ها و پایش خطاها</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>خلاصه مدیریتی،</strong><br>
این مستند به‌عنوان راهنمای فنی برای ارتقای ماژول <code>xLoggingService</code> تهیه شده است. هدف، افزودن سه قابلیت کلیدی به این ماژول است: (۱) یکپارچه‌سازی با OpenTelemetry برای رهگیری توزیع‌شده (Distributed Tracing)، (۲) ایجاد یک میدل‌ور (Middleware) برای لاگ کردن خودکار درخواست‌ها و خطاها با استفاده از سرویس لاگر موجود، و (۳) پیاده‌سازی گام‌به‌گام Sentry برای ثبت و رهگیری خطاها در سطح پروژه. هر بخش شامل توضیحات فنی، نمونه کد و نکات پیاده‌سازی است.
</div>
<!-- ================= SECTION 1: OpenTelemetry ================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>یکپارچه‌سازی OpenTelemetry با xLoggingService</h3>
<div class="subtitle">OpenTelemetry Integration for Distributed Tracing</div>
</div>
</div>
<div class="section-content">
<p>
OpenTelemetry یک چارچوب متن‌باز و استاندارد صنعتی برای جمع‌آوری داده‌های تلمتری (Logs، Metrics و Traces) است. با افزودن OpenTelemetry به <code>xLoggingService</code>، می‌توان لاگ‌ها را به‌صورت خودکار با TraceId و SpanId غنی‌سازی کرد و آن‌ها را در کنار سایر سیگنال‌های نظارتی (مانند Jaeger یا Zipkin) مشاهده نمود. این کار باعث می‌شود که بتوان یک درخواست را در سراسر سرویس‌های مختلف ردیابی کرد.
</p>
<div class="info-box">
<strong>💡 نکته کلیدی:</strong> در SDK دات‌نت، با فراخوانی <code>WithLogging</code>، به‌طور خودکار <code>OpenTelemetryLoggerProvider</code> ثبت می‌شود و لاگ‌های <code>ILogger</code> با <code>ActivityContext</code> (شامل TraceId و SpanId) غنی می‌شوند. همبستگی لاگ‌ها با Traceها به‌صورت خودکار انجام می‌شود و نیاز به اقدام اضافی نیست. [reference:0][reference:1]
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام‌های پیاده‌سازی</h4>
<p class="no-indent"><strong>گام ۱: نصب پکیج‌های موردنیاز</strong></p>
<pre><code>dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Instrumentation.AspNetCore</code></pre>
<p class="no-indent"><strong>گام ۲: پیکربندی در Program.cs</strong></p>
<p>در فایل <code>Program.cs</code>، سرویس OpenTelemetry را به‌صورت زیر پیکربندی کنید. توجه داشته باشید که <code>WithLogging</code> برای یکپارچه‌سازی لاگ‌ها و <code>WithTracing</code> برای رهگیری توزیع‌شده استفاده می‌شود.</p>
<pre><code>var builder = WebApplication.CreateBuilder(args);
// پاک کردن ارائه‌دهندگان پیش‌فرض لاگ (اختیاری)
builder.Logging.ClearProviders();
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService("xLoggingService"))
.WithLogging(logging => logging
.AddOtlpExporter()) // ارسال لاگ‌ها به OTLP
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter()); // ارسال Traceها به OTLP
var app = builder.Build();
// ... ادامه پیکربندی برنامه
app.Run();</code></pre>
<p class="no-indent"><strong>گام ۳: اطمینان از همبستگی لاگ‌ها با Traceها</strong></p>
<p>
پس از پیکربندی، هر لاگی که توسط <code>ILogger</code> (و در نتیجه <code>XLogger</code> شما) ثبت شود، به‌طور خودکار دارای فیلدهای <code>TraceId</code> و <code>SpanId</code> خواهد بود. این امر باعث می‌شود که در ابزارهای مشاهده‌پذیری (Observability) بتوانید لاگ‌های مرتبط با یک Trace خاص را فیلتر کنید.
</p>
<div class="success-box">
<strong>✅ نتیجه:</strong> با این یکپارچه‌سازی، ماژول <code>xLoggingService</code> شما نه‌تنها لاگ‌ها را به فایل و دیتابیس می‌فرستد، بلکه آن‌ها را به‌صورت استاندارد به هر بک‌اند سازگار با OTLP (مانند Jaeger، Grafana Tempo یا یک کلکتور OpenTelemetry) ارسال می‌کند. این کار امکان رهگیری کامل درخواست‌ها در معماری میکروسرویس را فراهم می‌آورد.
</div>
</div>
</div>
<!-- ================= SECTION 2: Middleware ================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>ایجاد میدل‌ور برای لاگ درخواست‌ها و خطاها</h3>
<div class="subtitle">Request & Exception Logging Middleware</div>
</div>
</div>
<div class="section-content">
<p>
یکی از بهترین روش‌ها برای ثبت خودکار درخواست‌های HTTP و خطاهای رخ‌داده، استفاده از یک میدل‌ور اختصاصی است. این میدل‌ور با استفاده از <code>ILogger</code> (که به <code>xLoggingService</code> متصل است) عمل کرده و از پراکندگی <code>try-catch</code> در کنترلرها جلوگیری می‌کند. در دات‌نت ۸ و ۹، پیاده‌سازی <code>IExceptionHandler</code> توصیه می‌شود؛ زیرا مدیریت متمرکز و سازگاری با میدل‌ور پیش‌فرض <code>UseExceptionHandler</code> را فراهم می‌کند. [reference:2]
</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">پیاده‌سازی میدل‌ور لاگینگ</h4>
<p class="no-indent"><strong>گام ۱: ایجاد کلاس میدل‌ور</strong></p>
<pre><code>using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace xLoggingService.Middleware
{
public class XRequestLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<XRequestLoggingMiddleware> _logger;
public XRequestLoggingMiddleware(
RequestDelegate next,
ILogger<XRequestLoggingMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
var stopwatch = Stopwatch.StartNew();
try
{
// لاگ شروع درخواست
_logger.LogInformation(
"Handling request: {Method} {Path}",
context.Request.Method,
context.Request.Path);
await _next(context);
// لاگ پایان موفق درخواست
stopwatch.Stop();
_logger.LogInformation(
"Finished handling request: {Method} {Path} in {Elapsed}ms",
context.Request.Method,
context.Request.Path,
stopwatch.ElapsedMilliseconds);
}
catch (Exception ex)
{
// لاگ خطا با جزئیات کامل
stopwatch.Stop();
_logger.LogError(ex,
"Request failed: {Method} {Path} after {Elapsed}ms",
context.Request.Method,
context.Request.Path,
stopwatch.ElapsedMilliseconds);
throw; // پرتاب مجدد برای مدیریت توسط میدل‌ورهای بالادستی
}
}
}
}</code></pre>
<p class="no-indent"><strong>گام ۲: ثبت میدل‌ور در Program.cs</strong></p>
<p>میدل‌ور را در خط لوله پردازش درخواست‌ها (Request Pipeline) ثبت کنید. بهتر است آن را در ابتدای خط لوله قرار دهید تا همه درخواست‌ها را پوشش دهد.</p>
<pre><code>var app = builder.Build();
// ثبت میدل‌ور سفارشی
app.UseMiddleware&lt;XRequestLoggingMiddleware&gt;();
// ... سایر میدل‌ورها (مانند UseRouting، UseAuthentication و ...)
app.MapControllers();
app.Run();</code></pre>
<div class="info-box">
<strong>💡 نکته:</strong> اگر از <code>IExceptionHandler</code> استفاده می‌کنید، می‌توانید لاگ خطا را در متد <code>TryHandleAsync</code> انجام دهید. در این صورت، برای جلوگیری از ثبت دوباره خطا توسط میدل‌ور پیش‌فرض، می‌توانید دسته‌بندی <code>Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware</code> را در تنظیمات لاگ فیلتر کنید. [reference:3]
</div>
<div class="success-box">
<strong>✅ نتیجه:</strong> با این میدل‌ور، تمامی درخواست‌های HTTP به‌صورت خودکار در سرویس لاگر شما ثبت می‌شوند. در صورت بروز خطا، جزئیات کامل (شامل <code>Exception</code> و مدت زمان اجرا) لاگ شده و از پراکندگی کدهای <code>try-catch</code> جلوگیری می‌شود.
</div>
</div>
</div>
<!-- ================= SECTION 3: Sentry ================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>پیاده‌سازی گام‌به‌گام Sentry برای رهگیری خطاها</h3>
<div class="subtitle">Step-by-Step Sentry Integration</div>
</div>
</div>
<div class="section-content">
<p>
Sentry یک پلتفرم قدرتمند برای پایش خطاها (Error Monitoring) و رهگیری عملکرد (Performance Tracing) است. با یکپارچه‌سازی Sentry در پروژه، خطاهای مدیریت‌نشده به‌صورت خودکار ثبت شده و همراه با جزئیات کامل (Stack Trace، Breadcrumbs و Context) در داشبورد Sentry نمایش داده می‌شوند. در ادامه، مراحل پیاده‌سازی به‌صورت گام‌به‌گام شرح داده می‌شود.
</p>
<div class="highlight-box">
<strong>⚠️ پیش‌نیاز:</strong> قبل از شروع، یک حساب کاربری در <a href="https://sentry.io" target="_blank">Sentry.io</a> ایجاد کنید و یک پروژه جدید از نوع ASP.NET Core بسازید تا <code>DSN</code> (Data Source Name) خود را دریافت کنید.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۱: نصب پکیج Sentry</h4>
<pre><code>dotnet add package Sentry.AspNetCore -v 6.1.0</code></pre>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۲: پیکربندی Sentry در Program.cs</h4>
<p>سرویس Sentry را با استفاده از متد <code>UseSentry</code> پیکربندی کنید. این متد به‌گونه‌ای طراحی شده که به‌طور خودکار خطاهای مدیریت‌نشده را شکار کند. [reference:4]</p>
<pre><code>var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(options =>
{
options.Dsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
?? "___YOUR_DSN___";
options.Debug = true; // فقط در محیط توسعه
options.SendDefaultPii = true; // ارسال اطلاعات کاربر (اختیاری)
options.TracesSampleRate = 1.0; // در تولید به 0.1 تا 0.2 کاهش دهید
options.MinimumBreadcrumbLevel = LogLevel.Debug;
options.MinimumEventLevel = LogLevel.Warning;
});
var app = builder.Build();
// ...
app.Run();</code></pre>
<p class="no-indent"><strong>گام ۳: پیکربندی از طریق appsettings.json (جایگزین)</strong></p>
<p>می‌توانید تنظیمات را در فایل <code>appsettings.json</code> نیز قرار دهید. این روش انعطاف‌پذیری بیشتری برای محیط‌های مختلف فراهم می‌کند. [reference:5]</p>
<pre><code>{
"Sentry": {
"Dsn": "___YOUR_DSN___",
"SendDefaultPii": true,
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"AttachStacktrace": true,
"Debug": false,
"TracesSampleRate": 1.0,
"Environment": "production",
"Release": "my-app@1.0.0"
}
}</code></pre>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۴: آپلود نمادهای دیباگ (Symbols) برای Stack Trace خوانا</h4>
<p>
بدون آپلود فایل‌های PDB، Stack Trace تنها نام متدها را نشان می‌دهد و فاقد شماره خط و نام فایل خواهد بود. برای رفع این مشکل، خواص MSBuild زیر را به فایل <code>.csproj</code> اضافه کنید. [reference:6]
</p>
<pre><code>&lt;PropertyGroup Condition="'$(Configuration)' == 'Release'"&gt;
&lt;SentryOrg&gt;___ORG_SLUG___&lt;/SentryOrg&gt;
&lt;SentryProject&gt;___PROJECT_SLUG___&lt;/SentryProject&gt;
&lt;SentryUploadSymbols&gt;true&lt;/SentryUploadSymbols&gt;
&lt;SentryUploadSources&gt;true&lt;/SentryUploadSources&gt;
&lt;SentryCreateRelease&gt;true&lt;/SentryCreateRelease&gt;
&lt;SentrySetCommits&gt;true&lt;/SentrySetCommits&gt;
&lt;/PropertyGroup&gt;</code></pre>
<p>برای آپلود، باید متغیر محیطی <code>SENTRY_AUTH_TOKEN</code> را در CI/CD تنظیم کنید. [reference:7]</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۵: تأیید و تست</h4>
<p>یک خطای آزمایشی ایجاد کنید تا از صحت پیکربندی مطمئن شوید. [reference:8]</p>
<pre><code>// یک Endpoint موقت برای تست
app.MapGet("/sentry-test", () =>
{
throw new Exception("Sentry test error — delete me");
});</code></pre>
<p>سپس به داشبورد Sentry (بخش Issues) بروید. خطا باید ظرف حدود ۳۰ ثانیه نمایش داده شود. [reference:9]</p>
<div class="warning-box">
<strong>⚠️ نکته امنیتی:</strong> هرگز <code>DSN</code> را به‌صورت مستقیم در کد منبع قرار ندهید. از متغیرهای محیطی یا <code>appsettings.json</code> (با تنظیمات مناسب) استفاده کنید. همچنین <code>SENTRY_AUTH_TOKEN</code> باید به‌عنوان یک Secret در CI/CD نگهداری شود.
</div>
<div class="success-box">
<strong>✅ نتیجه:</strong> با پیاده‌سازی Sentry، خطاهای پروژه به‌صورت خودکار در داشبورد Sentry ثبت می‌شوند. این امر امکان شناسایی سریع مشکلات، مشاهده Stack Trace خوانا و رهگیری خطاها در محیط تولید را فراهم می‌کند.
</div>
</div>
</div>
<!-- ================= SECTION 4: Summary Table ================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>جمع‌بندی و مقایسه یکپارچه‌سازی‌ها</h3>
<div class="subtitle">Summary of Integrations</div>
</div>
</div>
<div class="section-content">
<p>جدول زیر خلاصه‌ای از سه قابلیت اضافه‌شده و مزایای کلیدی هر یک را نشان می‌دهد:</p>
<table class="styled-table">
<thead>
<tr>
<th>قابلیت</th>
<th>هدف اصلی</th>
<th>مزیت کلیدی</th>
<th>وضعیت پیاده‌سازی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>OpenTelemetry</strong></td>
<td>رهگیری توزیع‌شده (Distributed Tracing)</td>
<td>همبستگی خودکار لاگ‌ها با Traceها و ارسال به بک‌اندهای استاندارد</td>
<td style="color: var(--success);">آماده</td>
</tr>
<tr>
<td><strong>میدل‌ور لاگینگ</strong></td>
<td>ثبت خودکار درخواست‌ها و خطاها</td>
<td>حذف <code>try-catch</code> تکراری و ثبت متمرکز رخدادها</td>
<td style="color: var(--success);">آماده</td>
</tr>
<tr>
<td><strong>Sentry</strong></td>
<td>پایش و رهگیری خطاها</td>
<td>Stack Trace خوانا، هشدارهای خودکار و داشبورد مدیریت خطا</td>
<td style="color: var(--success);">آماده</td>
</tr>
<tr class="highlight-row">
<td><strong>نتیجه نهایی</strong></td>
<td>مشاهده‌پذیری کامل (Full Observability)</td>
<td>ترکیب Logs، Metrics و Traces در یک اکوسیستم یکپارچه</td>
<td style="color: var(--success); font-weight: 700;">تکمیل</td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 جمع‌بندی نهایی</h3>
<p>
با پیاده‌سازی این سه قابلیت، ماژول <code>xLoggingService</code> از یک لاگر ساده به یک راه‌حل کامل مشاهده‌پذیری (Observability) ارتقا می‌یابد. OpenTelemetry امکان رهگیری توزیع‌شده را فراهم می‌کند، میدل‌ور لاگینگ ثبت خودکار درخواست‌ها را تضمین می‌کند و Sentry پایش خطاها را در سطح تولید ممکن می‌سازد. این ترکیب، تشخیص و رفع مشکلات را به‌طور چشمگیری تسریع کرده و به تیم توسعه کمک می‌کند تا با اطمینان بیشتری نرم‌افزار را در محیط عملیاتی اجرا کند.
</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
رهگیری توزیع‌شده + لاگ خودکار درخواست‌ها + پایش هوشمند خطاها = مشاهده‌پذیری کامل و کاهش زمان رفع اشکال
</div>
</div>
</div>
</div>
<!-- ================= SECTION 5: References ================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>منابع و مستندات</h3>
<div class="subtitle">References</div>
</div>
</div>
<div class="section-content">
<ul class="reference-list">
<li>
<strong>OpenTelemetry .NET SDK Documentation</strong>
راهنمای رسمی پیکربندی OpenTelemetry با <code>WithLogging</code> و <code>WithTracing</code>
<div class="ref-source">منبع: https://github.com/open-telemetry/opentelemetry-dotnet</div>
</li>
<li>
<strong>Microsoft Learn: Error Handling in ASP.NET Core</strong>
مدیریت خطا و پیاده‌سازی <code>IExceptionHandler</code>
<div class="ref-source">منبع: https://learn.microsoft.com/aspnet/core/fundamentals/error-handling</div>
</li>
<li>
<strong>Sentry .NET SDK Documentation</strong>
راهنمای گام‌به‌گام نصب و پیکربندی Sentry در ASP.NET Core
<div class="ref-source">منبع: https://docs.sentry.io/platforms/dotnet/</div>
</li>
<li>
<strong>Sentry for AI: .NET SDK Skill Guide</strong>
دستورالعمل جامع پیکربندی Sentry شامل Symbol Upload و گزینه‌های پیشرفته
<div class="ref-source">منبع: https://github.com/getsentry/sentry-for-ai</div>
</li>
</ul>
</div>
</div>
<div class="page-info">
این مستند فنی بوده و برای تیم توسعه <code>xLoggingService</code> تهیه شده است. هرگونه بازتولید یا انتشار بدون اجازه کتبی ممنوع است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">تهیه‌شده برای ارتقای ماژول xLoggingService</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: فنی</div>
</footer>
</div>
</body>
</html>