Skip to content
Closed
4 changes: 4 additions & 0 deletions eng/Signing.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<FileSignInfo Include = "Std.UriTemplate.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Spectre.Console.Cli.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Spectre.Console.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Serilog.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Serilog.Extensions.Logging.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Serilog.Sinks.Console.dll" CertificateName="3PartySHA2"/>
<FileSignInfo Include = "Serilog.Sinks.File.dll" CertificateName="3PartySHA2"/>
</ItemGroup>

<ItemGroup Label="Code sign exclusions">
Expand Down
4 changes: 2 additions & 2 deletions eng/Versions.DotNetScaffold.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionPrefix>9.0.1</VersionPrefix>
<VersionPrefix>9.0.1</VersionPrefix>
<PreReleaseVersionLabel>preview</PreReleaseVersionLabel>
<PreReleaseVersionIteration>10</PreReleaseVersionIteration>
<PreReleaseVersionIteration>1</PreReleaseVersionIteration>
<IncludeSourceRevisionInInformationalVersion>False</IncludeSourceRevisionInInformationalVersion>
<IsServicingBuild Condition="'$(PreReleaseVersionLabel)' == 'servicing'">true</IsServicingBuild>
<!--
Expand Down
2 changes: 2 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<MonoTextTemplatingVersion>3.0.0</MonoTextTemplatingVersion>
<SpectreConsoleFlowVersion>0.5.638</SpectreConsoleFlowVersion>
<SpectreConsoleVersion>0.47.0</SpectreConsoleVersion>
<SerilogPackageVersion>4.1.0</SerilogPackageVersion>
<SerilogExtensionsPackageVersion>8.0.0</SerilogExtensionsPackageVersion>
<MicrosoftCodeAnalysisPreviewVersion>4.12.0</MicrosoftCodeAnalysisPreviewVersion>
<MicrosoftCodeAnalysisVersion>4.12.0</MicrosoftCodeAnalysisVersion>
<SystemCommandLinePackageVersion>2.0.0-beta4.22613.1</SystemCommandLinePackageVersion>
Expand Down
3 changes: 3 additions & 0 deletions src/dotnet-scaffolding/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" />
<PackageVersion Include="Mono.TextTemplating" Version="$(MonoTextTemplatingVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" />
<PackageVersion Include="Serilog.Sinks.File" Version="$(SerilogPackageVersion)" />
<PackageVersion Include="Serilog.Sinks.Console" Version="$(SerilogPackageVersion)" />
<PackageVersion Include="Spectre.Console.Flow" Version="$(SpectreConsoleFlowVersion)" />
<PackageVersion Include="Spectre.Console" Version="$(SpectreConsoleVersion)" />
<PackageVersion Include="Spectre.Console.Cli" Version="$(SpectreConsoleVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Scaffolding.Core.CommandLine;
using Microsoft.DotNet.Scaffolding.Core.ComponentModel;
using Microsoft.DotNet.Scaffolding.Core.Logging;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace Microsoft.DotNet.Scaffolding.Core.Builder;

Expand All @@ -20,7 +23,6 @@ internal class ScaffoldRunnerBuilder : IScaffoldRunnerBuilder
public ScaffoldRunnerBuilder()
{
_logging = new LoggingBuilder(Services);

AddDefaultServices();
}

Expand Down Expand Up @@ -72,13 +74,49 @@ private void AddDefaultServices()

private void AddCoreServices()
{
Services.AddLogging();
Logging.AddCleanConsoleFormatter();
var loggerConfig = GetLoggingConfiguration();
Services.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddSerilog(loggerConfig.CreateLogger(), dispose: true);
});

Logging.AddDebug();
}

private LoggerConfiguration GetLoggingConfiguration()
{
//get some logging properties from environment variables
var isVerboseEnabled = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.ENABLE_VERBOSE_LOGGING);
var isLogToFileEnabled = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.LOG_TO_FILE);
// Configure Serilog Logger
var loggerConfig = new LoggerConfiguration().WriteTo.Sink(new AnsiConsoleSink());
loggerConfig.MinimumLevel.Information();
if (isVerboseEnabled)
{
loggerConfig.MinimumLevel.Verbose();
}

if (isLogToFileEnabled)
{
var loggingDirectory = Path.Combine(EnvironmentHelpers.GetUserProfilePath(), _defaultLogFolder);
if (!Directory.Exists(loggingDirectory))
{
Directory.CreateDirectory(loggingDirectory);
}

var filePath = $"dotnet-scaffold-{DateTime.Now:yyyy-MM-dd_HH-mm}.log";
var logPath = StringUtil.GetUniqueFilePath(Path.Combine(loggingDirectory, filePath));
loggerConfig.WriteTo.File(logPath, rollingInterval: RollingInterval.Infinite);
}

return loggerConfig;
}

private sealed class LoggingBuilder(IServiceCollection services) : ILoggingBuilder
{
public IServiceCollection Services { get; } = services;
}

private readonly string _defaultLogFolder = Path.Combine(".dotnet-scaffold", ".logs");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Microsoft.Extensions.Logging;

namespace Microsoft.DotNet.Scaffolding.Core.CommandLine;
Expand Down Expand Up @@ -28,7 +28,7 @@ public static bool AddPackage(string packageName, ILogger logger, string? projec
arguments.Add("--prerelease");
}

logger.LogInformation(string.Format("\nAdding package '{0}'...", packageName));
logger.LogInformation(string.Format("Adding package '{0}'...", packageName));
var runner = DotnetCliRunner.CreateDotNet("add", arguments);

// Buffer the output here because we'll only display it in the failure scenario
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace Microsoft.DotNet.Scaffolding.Core.ComponentModel;

internal static class ScaffolderConstants
{
internal const string LAUNCHED_BY_DOTNET_SCAFFOLD = "LAUNCHED_BY_DOTNET_SCAFFOLD";
internal const string DEFAULT_CATEGORY = "All";
internal const string ENABLE_VERBOSE_LOGGING = "ENABLE_VERBOSE_LOGGING";
internal const string LOG_TO_FILE = "LOG_TO_FILE";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Scaffolding.Core.ComponentModel;
using Microsoft.DotNet.Scaffolding.Internal.Shared;
using Serilog.Core;
using Serilog.Events;
using Spectre.Console;

namespace Microsoft.DotNet.Scaffolding.Core.Logging;

public class AnsiConsoleSink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
//LAUNCHED_BY_DOTNET_SCAFFOLD should be "true", or some other value. "true" indicates being launched by dotnet-scaffold.
bool isLaunchedByDotnetScaffold = EnvironmentHelpers.GetEnvironmentVariableAsBool(ScaffolderConstants.LAUNCHED_BY_DOTNET_SCAFFOLD);
var formattedMessage = FormatMessage(logEvent.Level, logEvent.RenderMessage());
if (isLaunchedByDotnetScaffold && (Console.IsOutputRedirected || Console.IsErrorRedirected))
{
// Output plain markup for main app to handle
AnsiConsole.WriteLine(formattedMessage);
}
else
{
// Render colored output directly
AnsiConsole.MarkupLine(formattedMessage);
}
}

private string FormatMessage(LogEventLevel level, string message)
{
return level switch
{
LogEventLevel.Verbose => $"[gray]{message}[/]",
LogEventLevel.Debug => $"[gray]{message}[/]",
LogEventLevel.Information => $"[default]{message}[/]",
LogEventLevel.Warning => $"[yellow]{message}[/]",
LogEventLevel.Error => $"[red]{message}[/]",
LogEventLevel.Fatal => $"[bold red]{message}[/]",
_ => message
};
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(BuildTfm)</TargetFrameworks>
<PackageTags>dotnet;scaffold;</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<NoWarn>$(NoWarn);8002</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Spectre.Console.Cli" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)src\dotnet-scaffolding\Microsoft.DotNet.Scaffolding.Internal\CliHelpers\*.cs">
<Link>CliHelpers\%(Filename)%(Extension)</Link>
<Compile Include="$(RepoRoot)src\dotnet-scaffolding\Microsoft.DotNet.Scaffolding.Internal\Shared\*.cs">
<Link>Shared\%(Filename)%(Extension)</Link>
</Compile>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ namespace Microsoft.DotNet.Scaffolding.Internal;

internal static class Constants
{
public static class EnvironmentVariables
{
public const string MSBuildExtensionsPath32 = nameof(MSBuildExtensionsPath32);
public const string MSBuildExtensionsPath = nameof(MSBuildExtensionsPath);
public const string MSBUILD_EXE_PATH = nameof(MSBUILD_EXE_PATH);
public const string MSBuildSDKsPath = nameof(MSBuildSDKsPath);
public const string USERPROFILE = nameof(USERPROFILE);
public const string VSINSTALLDIR = nameof(VSINSTALLDIR);
}

public static class StepConstants
{
public const string BaseProjectPath = nameof(BaseProjectPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Runtime.InteropServices;
using Microsoft.DotNet.Scaffolding.Internal.Shared;

namespace Microsoft.DotNet.Scaffolding.Internal.Services;
/// <summary>
Expand Down Expand Up @@ -33,16 +33,7 @@ public static string LocalNugetCachePath
}
}

public static string LocalUserProfilePath
{
get
{
return System.Environment.GetEnvironmentVariable(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "USERPROFILE"
: "HOME") ?? "USERPROFILE";
}
}
public static string LocalUserProfilePath => EnvironmentHelpers.GetUserProfilePath();

public const string DotnetProfileDirectoryName = ".dotnet";

Expand Down Expand Up @@ -123,25 +114,7 @@ public string GetMachineName()
/// <inheritdoc />
public bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
{
var str = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrEmpty(str))
{
return defaultValue;
}

switch (str.ToLowerInvariant())
{
case "true":
case "1":
case "yes":
return true;
case "false":
case "0":
case "no":
return false;
default:
return defaultValue;
}
return EnvironmentHelpers.GetEnvironmentVariableAsBool(name, defaultValue);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
using Microsoft.DotNet.Scaffolding.Internal.Shared;

namespace Microsoft.DotNet.Scaffolding.Internal.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;

namespace Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

internal static class ArgumentEscaper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;

namespace Microsoft.DotNet.Scaffolding.Internal.CliHelpers;
namespace Microsoft.DotNet.Scaffolding.Internal.Shared;

/// <summary>
/// To run 'dotnet' or any processes and capture consequent stdout and stderr (using 'ExecuteAndCaptureOutput'
Expand Down
Loading