forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbLogging.cs
More file actions
93 lines (87 loc) · 3.07 KB
/
Copy pathDbLogging.cs
File metadata and controls
93 lines (87 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Text.RegularExpressions;
using Npgsql;
using NpgsqlRest;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
namespace NpgsqlRestClient;
public class PostgresSink(
string command,
LogEventLevel restrictedToMinimumLevel,
int paramCount,
string? connectionString,
RetryStrategy? cmdRetryStrategy) : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
if (string.IsNullOrEmpty(connectionString) is true)
{
return;
}
if (logEvent.Level < restrictedToMinimumLevel)
{
return;
}
try
{
using var connection = new NpgsqlConnection(connectionString);
using var command1 = new NpgsqlCommand(command, connection);
if (paramCount > 0)
{
command1.Parameters.Add(new NpgsqlParameter() { Value = logEvent.Level.ToString() }); // $1
}
if (paramCount > 1)
{
command1.Parameters.Add(new NpgsqlParameter() { Value = logEvent.RenderMessage() }); // $2
}
if (paramCount > 2)
{
command1.Parameters.Add(new NpgsqlParameter() { Value = logEvent.Timestamp.UtcDateTime }); // $3
}
if (paramCount > 3)
{
command1.Parameters.Add(new NpgsqlParameter() { Value = logEvent.Exception?.ToString() ?? (object)DBNull.Value }); // $4
}
if (paramCount > 4)
{
command1.Parameters.Add(new NpgsqlParameter() { Value = logEvent.Properties["SourceContext"]?.ToString()?.Trim('"') ?? (object)DBNull.Value }); // $5
}
connection.Open();
command1.ExecuteNonQueryWithRetry(cmdRetryStrategy, null);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error writing to Postgres Log Sink:");
Console.WriteLine(ex);
Console.ResetColor();
}
}
}
public static partial class PostgresSinkSinkExtensions
{
public static LoggerConfiguration Postgres(
this LoggerSinkConfiguration loggerConfiguration,
string command,
LogEventLevel restrictedToMinimumLevel,
string? connectionString,
RetryStrategy? cmdRetryStrategy)
{
var matches = ParameterRegex().Matches(command).ToArray();
if (matches.Length < 1 || matches.Length > 5)
{
throw new ArgumentException("Command should have at least one parameter and maximum five parameters.");
}
for(int i = 0; i < matches.Length; i++)
{
if (matches[i].Value != $"${i + 1}")
{
throw new ArgumentException($"Parameter ${i + 1} is missing in the command.");
}
}
return loggerConfiguration.Sink(new PostgresSink(command, restrictedToMinimumLevel, matches.Length, connectionString, cmdRetryStrategy));
}
[GeneratedRegex(@"\$\d+")]
public static partial Regex ParameterRegex();
}