[dotnet/sdk] Use source context with serilog (#7095)

* Use source context with serilog

* Update changelog

* Remove framework reference

* Remove unavailable directives

Requires dotnet 5 or later.

* Use null-forgiving initialization
This commit is contained in:
Sean Fausett 2021-05-22 10:20:46 +12:00 committed by GitHub
parent 67bcd73a15
commit cc8459b2d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 31 deletions

View file

@ -1,5 +1,8 @@
### Improvements
- [dotnet/sdk] - Use source context with serilog
[#7095](https://github.com/pulumi/pulumi/pull/7095)
- [auto/dotnet] - Make StackDeployment.FromJsonString public
[#7067](https://github.com/pulumi/pulumi/pull/7067)

View file

@ -42,7 +42,7 @@ namespace Pulumi
/// </summary>
Task ILogger.DebugAsync(string message, Resource? resource, int? streamId, bool? ephemeral)
{
Serilog.Log.Debug(message);
_deployment.Serilogger.Debug(message);
return LogImplAsync(LogSeverity.Debug, message, resource, streamId, ephemeral);
}
@ -52,7 +52,7 @@ namespace Pulumi
/// </summary>
Task ILogger.InfoAsync(string message, Resource? resource, int? streamId, bool? ephemeral)
{
Serilog.Log.Information(message);
_deployment.Serilogger.Information(message);
return LogImplAsync(LogSeverity.Info, message, resource, streamId, ephemeral);
}
@ -61,7 +61,7 @@ namespace Pulumi
/// </summary>
Task ILogger.WarnAsync(string message, Resource? resource, int? streamId, bool? ephemeral)
{
Serilog.Log.Warning(message);
_deployment.Serilogger.Warning(message);
return LogImplAsync(LogSeverity.Warning, message, resource, streamId, ephemeral);
}
@ -74,7 +74,7 @@ namespace Pulumi
private Task ErrorAsync(string message, Resource? resource = null, int? streamId = null, bool? ephemeral = null)
{
Serilog.Log.Error(message);
_deployment.Serilogger.Error(message);
return LogImplAsync(LogSeverity.Error, message, resource, streamId, ephemeral);
}

View file

@ -69,7 +69,7 @@ namespace Pulumi
public void RegisterTask(string description, Task task)
{
Serilog.Log.Information($"Registering task: {description}");
_deployment.Serilogger.Information($"Registering task: {description}");
lock (_inFlightTasks)
{
@ -138,7 +138,7 @@ namespace Pulumi
}
foreach (var description in descriptions)
{
Serilog.Log.Information($"Completed task: {description}");
_deployment.Serilogger.Information($"Completed task: {description}");
}
// Check if all the tasks are completed and signal the completion source if so.
@ -188,7 +188,7 @@ namespace Pulumi
{
// We got an error while logging itself. Nothing to do here but print some errors
// and fail entirely.
Serilog.Log.Error(exception, "Error occurred trying to send logging message to engine.");
_deployment.Serilogger.Error(exception, "Error occurred trying to send logging message to engine.");
await Console.Error.WriteLineAsync("Error occurred trying to send logging message to engine:\n" + exception).ConfigureAwait(false);
return 1;
}
@ -220,7 +220,7 @@ namespace Pulumi
{exception.ToString()}").ConfigureAwait(false);
}
Serilog.Log.Debug("Wrote last error. Returning from program.");
_deployment.Serilogger.Debug("Wrote last error. Returning from program.");
return _processExitedAfterLoggingUserActionableMessage;
}
}

View file

@ -5,7 +5,10 @@ using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Pulumi.Testing;
using Serilog;
using Serilog.Events;
namespace Pulumi
{
@ -82,6 +85,7 @@ namespace Pulumi
private readonly bool _isDryRun;
private readonly ConcurrentDictionary<string, bool> _featureSupport = new ConcurrentDictionary<string, bool>();
private Serilog.ILogger _serilogger = null!;
private readonly ILogger _logger;
private readonly IRunner _runner;
@ -122,13 +126,15 @@ namespace Pulumi
_stackName = stack;
_projectName = project;
Serilog.Log.Debug("Creating Deployment Engine.");
this.Engine = new GrpcEngine(engine);
Serilog.Log.Debug("Created Deployment Engine.");
InitSerilogger();
Serilog.Log.Debug("Creating Deployment Monitor.");
_serilogger.Debug("Creating Deployment Engine.");
this.Engine = new GrpcEngine(engine);
_serilogger.Debug("Created Deployment Engine.");
_serilogger.Debug("Creating Deployment Monitor.");
this.Monitor = new GrpcMonitor(monitor);
Serilog.Log.Debug("Created Deployment Monitor.");
_serilogger.Debug("Created Deployment Monitor.");
_runner = new Runner(this);
_logger = new Logger(this, this.Engine);
@ -143,6 +149,7 @@ namespace Pulumi
/// </summary>
internal Deployment(IEngine engine, IMonitor monitor, TestOptions? options)
{
InitSerilogger();
_isDryRun = options?.IsPreview ?? true;
_stackName = options?.StackName ?? "stack";
_projectName = options?.ProjectName ?? "project";
@ -156,6 +163,7 @@ namespace Pulumi
string IDeployment.StackName => _stackName;
bool IDeployment.IsDryRun => _isDryRun;
Serilog.ILogger IDeploymentInternal.Serilogger => _serilogger;
ILogger IDeploymentInternal.Logger => _logger;
IRunner IDeploymentInternal.Runner => _runner;
@ -165,6 +173,22 @@ namespace Pulumi
set => Stack = value;
}
private void InitSerilogger()
{
var verboseLogging = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PULUMI_DOTNET_LOG_VERBOSE"));
var configRoot = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
_serilogger = new LoggerConfiguration()
.MinimumLevel.Is(verboseLogging ? LogEventLevel.Verbose : LogEventLevel.Fatal)
.ReadFrom.Configuration(configRoot)
.WriteTo.Console()
.CreateLogger()
.ForContext<Deployment>();
}
private async Task<bool> MonitorSupportsFeature(string feature)
{
if (!this._featureSupport.ContainsKey(feature))

View file

@ -26,13 +26,15 @@ namespace Pulumi
throw new InvalidOperationException("Inline execution was not provided the necessary parameters to run the Pulumi engine.");
}
Serilog.Log.Debug("Creating Deployment Engine.");
Engine = new GrpcEngine(settings.EngineAddr);
Serilog.Log.Debug("Created Deployment Engine.");
InitSerilogger();
Serilog.Log.Debug("Creating Deployment Monitor.");
_serilogger.Debug("Creating Deployment Engine.");
Engine = new GrpcEngine(settings.EngineAddr);
_serilogger.Debug("Created Deployment Engine.");
_serilogger.Debug("Creating Deployment Monitor.");
Monitor = new GrpcMonitor(settings.MonitorAddr);
Serilog.Log.Debug("Created Deployment Monitor.");
_serilogger.Debug("Created Deployment Monitor.");
_runner = new Runner(this);
_logger = new Logger(this, Engine);

View file

@ -4,9 +4,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Serilog;
using Pulumi.Testing;
namespace Pulumi
@ -184,19 +182,11 @@ namespace Pulumi
// this method *must* remain marked async
// in order to protect the scope of the AsyncLocal Deployment.Instance we cannot elide the task (return it early)
// if the task is returned early and not awaited, than it is possible for any code that runs before the eventual await
// to be executed synchronously and thus have multiple calls to one of the Run methods affecting eachothers Deployment.Instance
// to be executed synchronously and thus have multiple calls to one of the Run methods affecting each others Deployment.Instance
internal static async Task<int> CreateRunnerAndRunAsync(
Func<Deployment> deploymentFactory,
Func<IRunner, Task<int>> runAsync)
{
var enableVerboseLogging = Environment.GetEnvironmentVariable("PULUMI_DOTNET_LOG_VERBOSE");
if (enableVerboseLogging != null && enableVerboseLogging != "")
{
Serilog.Log.Logger = new Serilog.LoggerConfiguration().MinimumLevel.Debug().WriteTo.Console().CreateLogger();
}
Serilog.Log.Debug("Deployment.Run called.");
Serilog.Log.Debug("Creating new Deployment.");
var deployment = deploymentFactory();
Instance = new DeploymentInstance(deployment);
return await runAsync(deployment._runner).ConfigureAwait(false);

View file

@ -12,6 +12,7 @@ namespace Pulumi
Stack Stack { get; set; }
Serilog.ILogger Serilogger { get; }
ILogger Logger { get; }
IRunner Runner { get; }

View file

@ -30,6 +30,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.15" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="2.9.6">
<PrivateAssets>all</PrivateAssets>
@ -37,10 +38,11 @@
</PackageReference>
<PackageReference Include="OneOf" Version="2.1.151" />
<PackageReference Include="semver" Version="2.0.6" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Pulumi.Protobuf" Version="3.13.0" />
<PackageReference Include="Grpc" Version="2.37.0" />