-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
142 lines (116 loc) · 5.29 KB
/
Program.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using Serilog;
using Serilog.Core;
using Serilog.Sinks.Datadog.Logs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace ConsoleApp_NetCore
{
internal class Program
{
private static readonly string _apiKey = "";
private static readonly string _source = "serilog-to-datadog-poc";
private static readonly string _host = "serilog-to-datadog-poc";
private static readonly string _service = "serilog-to-datadog-poc";
public static string[] _tags = new string[] { "env:develop" };
public const string HARDCODED_SERILOGDEBUG_LOCATION = @"C:\Repos\tmp\LogsTest\SerilogDebug.txt";
public const string HARDCODED_LOGS_LOCATION = @"C:\Repos\tmp\LogsTest\log.json";
private static void Main(string[] args)
{
var file = File.CreateText(HARDCODED_SERILOGDEBUG_LOCATION);
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
Serilog.Debugging.SelfLog.Enable(Console.Error);
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
#region Issue 1 - LoggerConfiguration doesn't have url parameter named 'url'
////Documentation reference:
////https://docs.datadoghq.com/logs/log_collection/csharp/?tab=serilog#agentless-logging
//// This one is not working.
//Logger logger1 = new LoggerConfiguration(url: "URL")
//.WriteTo.DatadogLogs("<API_KEY>")
//.CreateLogger();
#endregion Issue 1 - LoggerConfiguration doesn't have url parameter named 'url'
#region Issue 2 - SelfLog is not working (File sink)
//var file = File.CreateText(HARDCODED_SERILOGDEBUG_LOCATION);
//Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
//Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
//Serilog.Debugging.SelfLog.Enable(Console.Error);
//Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
//ILogger logger = new LoggerConfiguration()
// .WriteTo
// .File(new JsonFormatter(renderMessage: true), HARDCODED_LOGS_LOCATION)
// .Enrich.WithThreadId()
// .Enrich.WithMachineName()
// .Enrich.FromLogContext()
// .CreateLogger();
//string message = $"Issue 2 [ConsoleApp-NetCore] POC info - {DateTime.Now.ToLongTimeString()}";
//logger.Warning(message);
//logger.Information(message);
//logger.Error(message);
//logger.Fatal(message);
#endregion Issue 2 - SelfLog is not working (File sink)
#region Issue 3 - sending to the datadog is not working
List<Logger> loggers = new List<Logger>();
List<int> allPorts = new List<int>() { 443, 1883, 10516 };
List<string> allEndpoints = new List<string>()
{
"agent-intake.logs.datadoghq.eu",
"agent-http-intake.logs.datadoghq.eu",
"http-intake.logs.datadoghq.eu",
"tcp-intake.logs.datadoghq.eu",
"lambda-intake.logs.datadoghq.eu",
"lambda-http-intake.logs.datadoghq.eu",
"functions-intake.logs.datadoghq.eu"
};
List<bool> boolFlags = new List<bool>() { true, false };
loggers.Add(CreateDataDogLogger());
foreach (string endpoint in allEndpoints)
{
foreach (int port in allPorts)
{
foreach (bool boolFlag1 in boolFlags)
{
foreach (bool boolFlag2 in boolFlags)
{
loggers.Add(CreateDataDogLoggerOverride(endpoint, port, boolFlag1, boolFlag2));
}
}
}
}
foreach (var logger in loggers)
{
logger.Warning($"Agentless test succeeded.");
logger.Information($"Agentless test succeeded.");
logger.Error($"Agentless test succeeded.");
logger.Fatal($"Agentless test succeeded.");
}
#endregion Issue 3 - sending to the datadog is not working
}
public static Logger CreateDataDogLogger()
{
return new LoggerConfiguration()
.WriteTo.DatadogLogs(_apiKey)
.CreateLogger();
}
public static Logger CreateDataDogLoggerOverride(string urlParam, int portParam, bool useSSLParam, bool useTCPParam)
{
var config = new DatadogConfiguration(
url: urlParam,
port: portParam,
useSSL: useSSLParam,
useTCP: useTCPParam);
var log = new LoggerConfiguration()
.WriteTo.DatadogLogs(
apiKey: _apiKey,
source: _source,
service: _service,
host: _host,
tags: _tags,
configuration: config
)
.CreateLogger();
return log;
}
}
}