Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 62dcff0

Browse files
author
stas
committed
make logs scoped
1 parent b7e90b3 commit 62dcff0

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

src/ApiService/ApiService/EnvironmentVariables.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ static EnvironmentVariables()
2222
//TODO: Add environment variable to control where to write logs to
2323
public static LogDestination[] LogDestinations { get; set; }
2424

25+
//TODO: Get this from Environment variable
26+
public static ApplicationInsights.DataContracts.SeverityLevel LogSeverityLevel() { return ApplicationInsights.DataContracts.SeverityLevel.Verbose; }
27+
2528
public static class AppInsights
2629
{
2730
public static string? AppId { get => Environment.GetEnvironmentVariable("APPINSIGHTS_APPID"); }

src/ApiService/ApiService/Log.cs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public interface ILogTracer
132132
void ForceFlush();
133133
void Info(string message);
134134
void Warning(string message);
135+
void Verbose(string message);
135136

136137
ILogTracer WithTag(string k, string v);
137138
ILogTracer WithTags((string, string)[]? tags);
@@ -155,11 +156,11 @@ public class LogTracer : ILogTracerInternal
155156
private Guid _correlationId;
156157
private List<ILog> _loggers;
157158
private Dictionary<string, string> _tags;
159+
private SeverityLevel _logSeverityLevel;
158160

159161
public Guid CorrelationId => _correlationId;
160162
public IReadOnlyDictionary<string, string> Tags => _tags;
161163

162-
163164
private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]? tags)
164165
{
165166
List<KeyValuePair<string, string>> converted = new List<KeyValuePair<string, string>>();
@@ -177,14 +178,17 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
177178
}
178179
}
179180

180-
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers) : this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers) { }
181+
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers, SeverityLevel logSeverityLevel) :
182+
this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers, logSeverityLevel)
183+
{ }
181184

182185

183-
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers)
186+
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers, SeverityLevel logSeverityLevel)
184187
{
185188
_correlationId = correlationId;
186189
_tags = new(tags);
187190
_loggers = loggers;
191+
_logSeverityLevel = logSeverityLevel;
188192
}
189193

190194
//Single threaded only
@@ -220,42 +224,66 @@ public ILogTracer WithTags((string, string)[]? tags)
220224
newTags[k] = v;
221225
}
222226
}
223-
return new LogTracer(CorrelationId, newTags, _loggers);
227+
return new LogTracer(CorrelationId, newTags, _loggers, _logSeverityLevel);
228+
}
229+
230+
public void Verbose(string message)
231+
{
232+
if (_logSeverityLevel >= SeverityLevel.Verbose)
233+
{
234+
var caller = GetCaller();
235+
foreach (var logger in _loggers)
236+
{
237+
logger.Log(CorrelationId, message, SeverityLevel.Verbose, Tags, caller);
238+
}
239+
}
224240
}
225241

226242
public void Info(string message)
227243
{
228-
var caller = GetCaller();
229-
foreach (var logger in _loggers)
244+
if (_logSeverityLevel >= SeverityLevel.Information)
230245
{
231-
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
246+
var caller = GetCaller();
247+
foreach (var logger in _loggers)
248+
{
249+
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
250+
}
232251
}
233252
}
234253

235254
public void Warning(string message)
236255
{
237-
var caller = GetCaller();
238-
foreach (var logger in _loggers)
256+
if (_logSeverityLevel >= SeverityLevel.Warning)
239257
{
240-
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
258+
var caller = GetCaller();
259+
foreach (var logger in _loggers)
260+
{
261+
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
262+
}
241263
}
242264
}
243265

244266
public void Error(string message)
245267
{
246-
var caller = GetCaller();
247-
foreach (var logger in _loggers)
268+
if (_logSeverityLevel >= SeverityLevel.Error)
248269
{
249-
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
270+
var caller = GetCaller();
271+
foreach (var logger in _loggers)
272+
{
273+
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
274+
}
250275
}
251276
}
252277

253278
public void Critical(string message)
254279
{
255-
var caller = GetCaller();
256-
foreach (var logger in _loggers)
280+
if (_logSeverityLevel >= SeverityLevel.Critical)
257281
{
258-
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
282+
var caller = GetCaller();
283+
foreach (var logger in _loggers)
284+
{
285+
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
286+
}
259287
}
260288
}
261289

src/ApiService/ApiService/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void Main()
6565
)
6666
.ConfigureServices((context, services) =>
6767
services
68-
.AddScoped<ILogTracer>(_ => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.NewGuid()))
68+
.AddScoped<ILogTracer>(_ => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.NewGuid(), logSeverityLevel: EnvironmentVariables.LogSeverityLevel()))
6969
.AddSingleton<INodeOperations, NodeOperations>()
7070
.AddSingleton<IEvents, Events>()
7171
.AddSingleton<IWebhookOperations, WebhookOperations>()

src/ApiService/ApiService/onefuzzlib/InstanceConfig.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ public async Async.Task Save(InstanceConfig config, bool isNew = false, bool req
6060
}
6161
}
6262

63-
if (r.IsOk)
64-
{
65-
await _events.SendEvent(new EventInstanceConfigUpdated(config));
66-
}
63+
await _events.SendEvent(new EventInstanceConfigUpdated(config));
6764
}
6865
}

src/ApiService/ApiService/onefuzzlib/orm/EntityConverter.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,16 @@ public T ToRecord<T>(TableEntity entity) where T : EntityBase
198198
{
199199
throw new Exception("invalid ");
200200
}
201-
202201
}
203202

204203
var fieldName = ef.columnName;
204+
var obj = entity[fieldName];
205+
if (obj == null)
206+
{
207+
return null;
208+
}
209+
var objType = obj.GetType();
210+
205211
if (ef.type == typeof(string))
206212
{
207213
return entity.GetString(fieldName);
@@ -245,10 +251,6 @@ public T ToRecord<T>(TableEntity entity) where T : EntityBase
245251
else
246252
{
247253
var value = entity.GetString(fieldName);
248-
if (value == null)
249-
{
250-
return null;
251-
}
252254
return JsonSerializer.Deserialize(value, ef.type, options: _options); ;
253255
}
254256
}

0 commit comments

Comments
 (0)