Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ApiService/ApiService/ApiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<WarningLevel>5</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.0.0" />
Expand Down
3 changes: 3 additions & 0 deletions src/ApiService/ApiService/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ static EnvironmentVariables()
//TODO: Add environment variable to control where to write logs to
public static LogDestination[] LogDestinations { get; set; }

//TODO: Get this from Environment variable
public static ApplicationInsights.DataContracts.SeverityLevel LogSeverityLevel() { return ApplicationInsights.DataContracts.SeverityLevel.Verbose; }

public static class AppInsights
{
public static string? AppId { get => Environment.GetEnvironmentVariable("APPINSIGHTS_APPID"); }
Expand Down
40 changes: 0 additions & 40 deletions src/ApiService/ApiService/Info.cs

This file was deleted.

114 changes: 88 additions & 26 deletions src/ApiService/ApiService/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,34 @@ public interface ILogTracer
void ForceFlush();
void Info(string message);
void Warning(string message);
void Verbose(string message);

ILogTracer AddTags((string, string)[]? tags);
ILogTracer WithTag(string k, string v);
ILogTracer WithTags((string, string)[]? tags);
}

public class LogTracer : ILogTracer
internal interface ILogTracerInternal : ILogTracer
{
void ReplaceCorrelationId(Guid newCorrelationId);
void AddTags((string, string)[] tags);
}



public class LogTracer : ILogTracerInternal
{
private string? GetCaller()
{
return new StackTrace()?.GetFrame(2)?.GetMethod()?.DeclaringType?.FullName;
}

private Guid _correlationId;
private List<ILog> _loggers;
private Dictionary<string, string> _tags;
private SeverityLevel _logSeverityLevel;

public Guid CorrelationId { get; }
public IReadOnlyDictionary<string, string> Tags { get; }

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

private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]? tags)
{
Expand All @@ -166,17 +178,43 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
}
}

public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers) : this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers) { }
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers, SeverityLevel logSeverityLevel) :
this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers, logSeverityLevel)
{ }


public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers)
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers, SeverityLevel logSeverityLevel)
{
CorrelationId = correlationId;
Tags = tags;
_correlationId = correlationId;
_tags = new(tags);
_loggers = loggers;
_logSeverityLevel = logSeverityLevel;
}

//Single threaded only
public void ReplaceCorrelationId(Guid newCorrelationId)
{
_correlationId = newCorrelationId;
}

//single threaded only
public void AddTags((string, string)[] tags)
{
if (tags is not null)
{
foreach (var (k, v) in tags)
{
_tags[k] = v;
}
}
}

public ILogTracer AddTags((string, string)[]? tags)
public ILogTracer WithTag(string k, string v)
{
return WithTags(new[] { (k, v) });
}

public ILogTracer WithTags((string, string)[]? tags)
{
var newTags = new Dictionary<string, string>(Tags);
if (tags is not null)
Expand All @@ -186,42 +224,66 @@ public ILogTracer AddTags((string, string)[]? tags)
newTags[k] = v;
}
}
return new LogTracer(CorrelationId, newTags, _loggers);
return new LogTracer(CorrelationId, newTags, _loggers, _logSeverityLevel);
}

public void Verbose(string message)
{
if (_logSeverityLevel >= SeverityLevel.Verbose)
{
var caller = GetCaller();
foreach (var logger in _loggers)
{
logger.Log(CorrelationId, message, SeverityLevel.Verbose, Tags, caller);
}
}
}

public void Info(string message)
{
var caller = GetCaller();
foreach (var logger in _loggers)
if (_logSeverityLevel >= SeverityLevel.Information)
{
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
var caller = GetCaller();
foreach (var logger in _loggers)
{
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
}
}
}

public void Warning(string message)
{
var caller = GetCaller();
foreach (var logger in _loggers)
if (_logSeverityLevel >= SeverityLevel.Warning)
{
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
var caller = GetCaller();
foreach (var logger in _loggers)
{
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
}
}
}

public void Error(string message)
{
var caller = GetCaller();
foreach (var logger in _loggers)
if (_logSeverityLevel >= SeverityLevel.Error)
{
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
var caller = GetCaller();
foreach (var logger in _loggers)
{
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
}
}
}

public void Critical(string message)
{
var caller = GetCaller();
foreach (var logger in _loggers)
if (_logSeverityLevel >= SeverityLevel.Critical)
{
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
var caller = GetCaller();
foreach (var logger in _loggers)
{
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
}
}
}

Expand Down Expand Up @@ -254,7 +316,7 @@ public void ForceFlush()

public interface ILogTracerFactory
{
LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null);
LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose);
}

public class LogTracerFactory : ILogTracerFactory
Expand All @@ -266,9 +328,9 @@ public LogTracerFactory(List<ILog> loggers)
_loggers = loggers;
}

public LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null)
public LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose)
{
return new(correlationId, tags, _loggers);
return new(correlationId, tags, _loggers, severityLevel);
}

}
3 changes: 2 additions & 1 deletion src/ApiService/ApiService/OneFuzzTypes/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public enum ErrorCode
namespace Microsoft.OneFuzz.Service;
public enum ErrorCode
{
INVALID_REQUEST = 450,
INVALID_PERMISSION = 451,
Expand Down
7 changes: 4 additions & 3 deletions src/ApiService/ApiService/OneFuzzTypes/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public EventType GetEventType()
this switch
{
EventNodeHeartbeat _ => EventType.NodeHeartbeat,
EventInstanceConfigUpdated _ => EventType.InstanceConfigUpdated,
_ => throw new NotImplementedException(),
};

Expand Down Expand Up @@ -243,7 +244,7 @@ PoolName PoolName
// ) : BaseEvent();


// record EventInstanceConfigUpdated(
// InstanceConfig Config
// ) : BaseEvent();
record EventInstanceConfigUpdated(
InstanceConfig Config
) : BaseEvent();
}
Loading