Files
EonaCat.Logger/EonaCat.Logger/EonaCatCoreLogger/TcpLogger.cs

92 lines
3.1 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
namespace EonaCat.Logger.EonaCatCoreLogger
{
public class TcpLogger : ILogger
{
private readonly string _categoryName;
private readonly TcpLoggerOptions _options;
private readonly LoggerScopedContext _context = new();
public bool IncludeCorrelationId { get; set; }
public event EventHandler<Exception> OnException;
public TcpLogger(string categoryName, TcpLoggerOptions options)
{
_categoryName = categoryName;
_options = options;
IncludeCorrelationId = options.IncludeCorrelationId;
}
public void SetContext(string key, string value) => _context.Set(key, value);
public void ClearContext() => _context.Clear();
public string GetContext(string key) => _context.Get(key);
public IDisposable BeginScope<TState>(TState state) => null;
public bool IsEnabled(LogLevel logLevel) => _options.IsEnabled;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
try
{
string message = formatter(state, exception);
if (IncludeCorrelationId)
{
var correlationId = _context.Get("CorrelationId") ?? Guid.NewGuid().ToString();
_context.Set("CorrelationId", correlationId);
}
var logParts = new List<string>
{
$"[{DateTime.UtcNow:u}]",
$"[{logLevel}]",
$"[{_categoryName}]",
$"Message: {message}",
};
var contextData = _context.GetAll();
if (contextData.Count > 0)
{
logParts.Add("Context:");
foreach (var kvp in contextData)
{
logParts.Add($"{kvp.Key}: {kvp.Value}");
}
}
if (exception != null)
{
logParts.Add($"Exception: {exception}");
}
string fullLog = string.Join(" | ", logParts);
using var client = new TcpClient();
client.Connect(_options.Host, _options.Port);
using var stream = client.GetStream();
using var writer = new StreamWriter(stream, System.Text.Encoding.UTF8)
{
AutoFlush = true
};
writer.WriteLine(fullLog);
}
catch (Exception e)
{
OnException?.Invoke(this, e);
}
}
}
}