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

Commit 780ef24

Browse files
author
stas
committed
make logs scoped
1 parent b7e90b3 commit 780ef24

File tree

5 files changed

+57
-26
lines changed

5 files changed

+57
-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: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.ApplicationInsights.DataContracts;
66

77
using System.Diagnostics;
8+
using Microsoft.Extensions.DependencyInjection;
89

910
namespace Microsoft.OneFuzz.Service;
1011

@@ -132,6 +133,7 @@ public interface ILogTracer
132133
void ForceFlush();
133134
void Info(string message);
134135
void Warning(string message);
136+
void Verbose(string message);
135137

136138
ILogTracer WithTag(string k, string v);
137139
ILogTracer WithTags((string, string)[]? tags);
@@ -155,11 +157,11 @@ public class LogTracer : ILogTracerInternal
155157
private Guid _correlationId;
156158
private List<ILog> _loggers;
157159
private Dictionary<string, string> _tags;
160+
private SeverityLevel _logSeverityLevel;
158161

159162
public Guid CorrelationId => _correlationId;
160163
public IReadOnlyDictionary<string, string> Tags => _tags;
161164

162-
163165
private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]? tags)
164166
{
165167
List<KeyValuePair<string, string>> converted = new List<KeyValuePair<string, string>>();
@@ -177,14 +179,17 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
177179
}
178180
}
179181

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

182186

183-
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers)
187+
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers, SeverityLevel logSeverityLevel)
184188
{
185189
_correlationId = correlationId;
186190
_tags = new(tags);
187191
_loggers = loggers;
192+
_logSeverityLevel = logSeverityLevel;
188193
}
189194

190195
//Single threaded only
@@ -220,42 +225,66 @@ public ILogTracer WithTags((string, string)[]? tags)
220225
newTags[k] = v;
221226
}
222227
}
223-
return new LogTracer(CorrelationId, newTags, _loggers);
228+
return new LogTracer(CorrelationId, newTags, _loggers, _logSeverityLevel);
229+
}
230+
231+
public void Verbose(string message)
232+
{
233+
if (_logSeverityLevel >= SeverityLevel.Verbose)
234+
{
235+
var caller = GetCaller();
236+
foreach (var logger in _loggers)
237+
{
238+
logger.Log(CorrelationId, message, SeverityLevel.Verbose, Tags, caller);
239+
}
240+
}
224241
}
225242

226243
public void Info(string message)
227244
{
228-
var caller = GetCaller();
229-
foreach (var logger in _loggers)
245+
if (_logSeverityLevel >= SeverityLevel.Information)
230246
{
231-
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
247+
var caller = GetCaller();
248+
foreach (var logger in _loggers)
249+
{
250+
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
251+
}
232252
}
233253
}
234254

235255
public void Warning(string message)
236256
{
237-
var caller = GetCaller();
238-
foreach (var logger in _loggers)
257+
if (_logSeverityLevel >= SeverityLevel.Warning)
239258
{
240-
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
259+
var caller = GetCaller();
260+
foreach (var logger in _loggers)
261+
{
262+
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
263+
}
241264
}
242265
}
243266

244267
public void Error(string message)
245268
{
246-
var caller = GetCaller();
247-
foreach (var logger in _loggers)
269+
if (_logSeverityLevel >= SeverityLevel.Error)
248270
{
249-
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
271+
var caller = GetCaller();
272+
foreach (var logger in _loggers)
273+
{
274+
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
275+
}
250276
}
251277
}
252278

253279
public void Critical(string message)
254280
{
255-
var caller = GetCaller();
256-
foreach (var logger in _loggers)
281+
if (_logSeverityLevel >= SeverityLevel.Critical)
257282
{
258-
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
283+
var caller = GetCaller();
284+
foreach (var logger in _loggers)
285+
{
286+
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
287+
}
259288
}
260289
}
261290

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)