commit 4176ff84fe465e43d83ad10aa59e530fda20fdb1 parent d06ce24bc91d8f2226ed3e998db2affce12a3ea8 Author: sachintaMSFT <80828309+sachintaMSFT@users.noreply.github.com> Date: Tue, 31 Aug 2021 11:53:09 -0700 Update Loggers to support multiple Win32 app installs in single process (#1399) * Update Loggers to support multi threaded processing of Win32 app install requests in WPMServer Diffstat:
18 files changed, 243 insertions(+), 92 deletions(-)
diff --git a/.github/actions/spelling/allow.txt b/.github/actions/spelling/allow.txt @@ -44,6 +44,7 @@ bytearray cdecl cer certutil +cguid chrono cin cla @@ -252,6 +253,7 @@ MANIFESTSCHEMA MANIFESTVERSION MAXLENGTH MBs +MContext mday memset metadata diff --git a/src/AppInstallerCLICore/COMContext.cpp b/src/AppInstallerCLICore/COMContext.cpp @@ -51,14 +51,18 @@ namespace AppInstaller::CLI::Execution Logging::SetExecutionStage(static_cast<uint32_t>(m_executionStage)); } - void COMContext::SetLoggerContext(const std::wstring_view telemetryCorrelationJson, const std::string& caller) + void COMContext::SetContextLoggers(const std::wstring_view telemetryCorrelationJson, const std::string& caller) { m_correlationData = telemetryCorrelationJson; - Logging::Telemetry().SetTelemetryCorrelationJson(telemetryCorrelationJson); - Logging::Telemetry().SetCaller(caller); - Logging::Telemetry().LogStartup(true); + + std::unique_ptr<AppInstaller::ThreadLocalStorage::PreviousThreadGlobals> setThreadGlobalsToPreviousState = GetThreadGlobals().SetForCurrentThread(); + + SetLoggers(); + GetThreadGlobals().GetTelemetryLogger().SetTelemetryCorrelationJson(telemetryCorrelationJson); + GetThreadGlobals().GetTelemetryLogger().SetCaller(caller); + GetThreadGlobals().GetTelemetryLogger().LogStartup(true); } - + std::wstring_view COMContext::GetCorrelationJson() { return m_correlationData; @@ -66,7 +70,7 @@ namespace AppInstaller::CLI::Execution void COMContext::SetLoggers() { - Logging::Log().SetLevel(Logging::Level::Verbose); + Logging::Log().SetLevel(Logging::Level::Info); Logging::Log().EnableChannel(Logging::Channel::All); // TODO: Log to file for COM API calls only when debugging in visual studio diff --git a/src/AppInstallerCLICore/COMContext.h b/src/AppInstallerCLICore/COMContext.h @@ -7,7 +7,7 @@ namespace AppInstaller::CLI::Execution { - enum class ReportType: uint32_t + enum class ReportType : uint32_t { ExecutionPhaseUpdate, BeginProgress, @@ -42,7 +42,7 @@ namespace AppInstaller::CLI::Execution SetFlags(CLI::Execution::ContextFlag::AgreementsAcceptedByCaller); } - COMContext(std::ostream& out, std::istream& in) : CLI::Execution::Context(out, in) + COMContext(std::ostream& out, std::istream& in) : CLI::Execution::Context(out, in) { Reporter.SetProgressSink(this); SetFlags(CLI::Execution::ContextFlag::AgreementsAcceptedByCaller); @@ -62,19 +62,18 @@ namespace AppInstaller::CLI::Execution void AddProgressCallbackFunction(ProgressCallBackFunction&& f); - // Set COM call context for diagnostic and telemetry loggers - // This should be called for every COMContext object instance - void SetLoggerContext(const std::wstring_view telemetryCorrelationJson, const std::string& caller); - - std::wstring_view GetCorrelationJson(); - // Set Diagnostic and Telemetry loggers, Wil failure callback // This should be called only once per COM Server instance static void SetLoggers(); + // Set COM call context for diagnostic and telemetry loggers + // This should be called for every COMContext object instance + void SetContextLoggers(const std::wstring_view telemetryCorrelationJson, const std::string& caller); + + std::wstring_view GetCorrelationJson(); + private: void FireCallbacks(ReportType reportType, uint64_t current, uint64_t maximum, ProgressType progressType, ::AppInstaller::CLI::Workflow::ExecutionStage executionPhase); - std::vector<ProgressCallBackFunction> GetCallbacks(); CLI::Workflow::ExecutionStage m_executionStage = CLI::Workflow::ExecutionStage::Initial; std::vector<ProgressCallBackFunction> m_comProgressCallbacks; diff --git a/src/AppInstallerCLICore/ContextOrchestrator.cpp b/src/AppInstallerCLICore/ContextOrchestrator.cpp @@ -102,8 +102,11 @@ namespace AppInstaller::CLI::Execution try { ::AppInstaller::CLI::RootCommand rootCommand; + + std::unique_ptr<AppInstaller::ThreadLocalStorage::PreviousThreadGlobals> setThreadGlobalsToPreviousState = item->GetContext().GetThreadGlobals().SetForCurrentThread(); + std::unique_ptr<::AppInstaller::CLI::Command> command = std::make_unique<::AppInstaller::CLI::COMInstallCommand>(rootCommand.Name()); - ::AppInstaller::Logging::Telemetry().LogCommand(command->FullName()); + item->GetContext().GetThreadGlobals().GetTelemetryLogger().LogCommand(command->FullName()); command->ValidateArguments(item->GetContext().Args); item->GetContext().EnableCtrlHandler(); diff --git a/src/AppInstallerCLICore/ExecutionContext.cpp b/src/AppInstallerCLICore/ExecutionContext.cpp @@ -205,4 +205,10 @@ namespace AppInstaller::CLI::Execution m_executionStage = stage; Logging::SetExecutionStage(static_cast<uint32_t>(m_executionStage)); } + + AppInstaller::ThreadLocalStorage::ThreadGlobals& Context::GetThreadGlobals() + { + return m_threadGlobals; + } + } diff --git a/src/AppInstallerCLICore/ExecutionContext.h b/src/AppInstallerCLICore/ExecutionContext.h @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #pragma once -#include <AppInstallerLogging.h> +#include "winget/ThreadGlobals.h" #include "ExecutionReporter.h" #include "ExecutionArgs.h" #include "ExecutionContextData.h" @@ -123,6 +123,9 @@ namespace AppInstaller::CLI::Execution virtual void SetExecutionStage(Workflow::ExecutionStage stage, bool); + // Get Globals for Current Thread + AppInstaller::ThreadLocalStorage::ThreadGlobals& GetThreadGlobals(); + #ifndef AICLI_DISABLE_TEST_HOOKS // Enable tests to override behavior virtual bool ShouldExecuteWorkflowTask(const Workflow::WorkflowTask&) { return true; } @@ -135,5 +138,6 @@ namespace AppInstaller::CLI::Execution size_t m_CtrlSignalCount = 0; ContextFlag m_flags = ContextFlag::None; Workflow::ExecutionStage m_executionStage = Workflow::ExecutionStage::Initial; + AppInstaller::ThreadLocalStorage::ThreadGlobals m_threadGlobals; }; } diff --git a/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj b/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj @@ -302,6 +302,7 @@ <ClInclude Include="Public\winget\ManifestSchemaValidation.h" /> <ClInclude Include="Public\winget\Resources.h" /> <ClInclude Include="Public\winget\Settings.h" /> + <ClInclude Include="Public\winget\ThreadGlobals.h" /> <ClInclude Include="Public\winget\TraceLogger.h" /> <ClInclude Include="Public\winget\UserSettings.h" /> <ClInclude Include="Public\winget\Yaml.h" /> @@ -363,6 +364,7 @@ <ClCompile Include="Synchronization.cpp" /> <ClCompile Include="Telemetry\TraceLogging.cpp" /> <ClCompile Include="Architecture.cpp" /> + <ClCompile Include="ThreadGlobals.cpp" /> <ClCompile Include="TraceLogger.cpp" /> <ClCompile Include="UserSettings.cpp" /> <ClCompile Include="Versions.cpp" /> diff --git a/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj.filters b/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj.filters @@ -177,6 +177,9 @@ <ClInclude Include="Public\winget\Msi.h"> <Filter>Public\winget</Filter> </ClInclude> + <ClInclude Include="Public\winget\ThreadGlobals.h"> + <Filter>Public\winget</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="pch.cpp"> @@ -299,6 +302,9 @@ <ClCompile Include="TraceLogger.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="ThreadGlobals.cpp"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <None Include="PropertySheet.props" /> diff --git a/src/AppInstallerCommonCore/AppInstallerLogging.cpp b/src/AppInstallerCommonCore/AppInstallerLogging.cpp @@ -8,6 +8,7 @@ #include "Public/AppInstallerTelemetry.h" #include "Public/AppInstallerDateTime.h" #include "Public/AppInstallerRuntime.h" +#include "Public/winget/ThreadGlobals.h" namespace AppInstaller::Logging { @@ -49,12 +50,6 @@ namespace AppInstaller::Logging size_t GetMaxChannelNameLength() { return 4; } - DiagnosticLogger& DiagnosticLogger::GetInstance() - { - static DiagnosticLogger instance; - return instance; - } - void DiagnosticLogger::AddLogger(std::unique_ptr<ILogger>&& logger) { m_loggers.emplace_back(std::move(logger)); @@ -130,6 +125,20 @@ namespace AppInstaller::Logging } } + DiagnosticLogger& Log() + { + ThreadLocalStorage::ThreadGlobals* pThreadGlobals = ThreadLocalStorage::ThreadGlobals::GetForCurrentThread(); + if (pThreadGlobals) + { + return pThreadGlobals->GetDiagnosticLogger(); + } + else + { + static DiagnosticLogger processGlobalLogger; + return processGlobalLogger; + } + } + void AddFileLogger() { Log().AddLogger(std::make_unique<FileLogger>()); diff --git a/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp b/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp @@ -7,6 +7,7 @@ #include "Public/AppInstallerSHA256.h" #include "Public/AppInstallerStrings.h" #include "winget/UserSettings.h" +#include "Public/winget/ThreadGlobals.h" #define AICLI_TraceLoggingStringView(_sv_,_name_) TraceLoggingCountedUtf8String(_sv_.data(), static_cast<ULONG>(_sv_.size()), _name_) #define AICLI_TraceLoggingWStringView(_sv_,_name_) TraceLoggingCountedWideString(_sv_.data(), static_cast<ULONG>(_sv_.size()), _name_) @@ -14,7 +15,7 @@ #define AICLI_TraceLoggingWriteActivity(_eventName_,...) TraceLoggingWriteActivity(\ g_hTraceProvider,\ _eventName_,\ -GetActivityId(false),\ +GetActivityId(),\ nullptr,\ TraceLoggingCountedUtf8String(m_caller.c_str(), static_cast<ULONG>(m_caller.size()), "Caller"),\ TraceLoggingPackedFieldEx(m_telemetryCorrelationJsonW.c_str(), static_cast<ULONG>((m_telemetryCorrelationJsonW.size() + 1) * sizeof(wchar_t)), TlgInUNICODESTRING, TlgOutJSON, "CvJson"),\ @@ -55,48 +56,16 @@ namespace AppInstaller::Logging { Telemetry().LogFailure(info); } - - GUID CreateGuid() - { - GUID result{}; - (void)CoCreateGuid(&result); - return result; - } - } - - const GUID* GetActivityId(bool isNewActivity) - { - static GUID activityId; - if (isNewActivity == true) - { - activityId = CreateGuid(); - } - return &activityId; - } - - void SetActivityId() - { - GetActivityId(true); } TelemetryTraceLogger::TelemetryTraceLogger() { - // TODO: Needs to be made a singleton registration/removal in the future - RegisterTraceLogging(); - - m_isSettingEnabled = !Settings::User().Get<Settings::Setting::TelemetryDisable>(); - m_userProfile = Runtime::GetPathTo(Runtime::PathName::UserProfile).wstring(); + std::ignore = CoCreateGuid(&m_activityId); } - - TelemetryTraceLogger::~TelemetryTraceLogger() + + const GUID* TelemetryTraceLogger::GetActivityId() const { - UnRegisterTraceLogging(); - } - - TelemetryTraceLogger& TelemetryTraceLogger::GetInstance() - { - static TelemetryTraceLogger instance; - return instance; + return &m_activityId; } bool TelemetryTraceLogger::DisableRuntime() @@ -109,6 +78,12 @@ namespace AppInstaller::Logging m_isRuntimeEnabled = true; } + void TelemetryTraceLogger::Initialize() + { + m_isSettingEnabled = !Settings::User().Get<Settings::Setting::TelemetryDisable>(); + m_userProfile = Runtime::GetPathTo(Runtime::PathName::UserProfile).wstring(); + } + void TelemetryTraceLogger::SetTelemetryCorrelationJson(const std::wstring_view jsonStr_view) noexcept { // Check if passed in string is a valid Json formatted before returning the value @@ -179,8 +154,6 @@ namespace AppInstaller::Logging packageVersion = Runtime::GetPackageVersion(); } - Logging::SetActivityId(); - if (IsTelemetryEnabled()) { AICLI_TraceLoggingWriteActivity( @@ -192,7 +165,7 @@ namespace AppInstaller::Logging TraceLoggingKeyword(MICROSOFT_KEYWORD_CRITICAL_DATA)); } - AICLI_LOG(Core, Info, << "WinGet, version [" << version << "], activity [" << *GetActivityId(false) << ']'); + AICLI_LOG(Core, Info, << "WinGet, version [" << version << "], activity [" << *GetActivityId() << ']'); AICLI_LOG(Core, Info, << "OS: " << Runtime::GetOSVersion()); AICLI_LOG(Core, Info, << "Command line Args: " << Utility::ConvertToUTF8(GetCommandLineW())); if (Runtime::IsRunningInPackagedContext()) @@ -541,7 +514,8 @@ namespace AppInstaller::Logging TraceLoggingUInt32(s_subExecutionId, "SubExecutionId"), AICLI_TraceLoggingStringView(url, "Url"), TraceLoggingHResult(hr, "HResult"), - TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance)); + TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance), + TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES)); } } @@ -573,8 +547,16 @@ namespace AppInstaller::Logging return *s_TelemetryTraceLogger_TestOverride.get(); } #endif - - return TelemetryTraceLogger::GetInstance(); + ThreadLocalStorage::ThreadGlobals* pThreadGlobals = ThreadLocalStorage::ThreadGlobals::GetForCurrentThread(); + if (pThreadGlobals) + { + return pThreadGlobals->GetTelemetryLogger(); + } + else + { + static GlobalTelemetryTraceLogger processGlobalTelemetry; + return processGlobalTelemetry; + } } void EnableWilFailureTelemetry() diff --git a/src/AppInstallerCommonCore/Public/AppInstallerLogging.h b/src/AppInstallerCommonCore/Public/AppInstallerLogging.h @@ -72,6 +72,8 @@ namespace AppInstaller::Logging // desired level, as nothing is enabled by default. struct DiagnosticLogger { + DiagnosticLogger() = default; + ~DiagnosticLogger() = default; DiagnosticLogger(const DiagnosticLogger&) = delete; @@ -119,18 +121,13 @@ namespace AppInstaller::Logging void Write(Channel channel, Level level, std::string_view message); private: - DiagnosticLogger() = default; std::vector<std::unique_ptr<ILogger>> m_loggers; uint64_t m_enabledChannels = 0; Level m_enabledLevel = Level::Info; }; - // Helper to make the call sites look clean. - inline DiagnosticLogger& Log() - { - return DiagnosticLogger::GetInstance(); - } + DiagnosticLogger& Log(); // Adds the default file logger to the DiagnosticLogger. void AddFileLogger(); diff --git a/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h b/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h @@ -6,6 +6,7 @@ #include <string_view> #include <vector> +#include <cguid.h> namespace AppInstaller::Logging { @@ -15,7 +16,9 @@ namespace AppInstaller::Logging // this should not become a burden. struct TelemetryTraceLogger { - virtual ~TelemetryTraceLogger(); + TelemetryTraceLogger(); + + ~TelemetryTraceLogger() = default; TelemetryTraceLogger(const TelemetryTraceLogger&) = default; TelemetryTraceLogger& operator=(const TelemetryTraceLogger&) = default; @@ -23,13 +26,16 @@ namespace AppInstaller::Logging TelemetryTraceLogger(TelemetryTraceLogger&&) = default; TelemetryTraceLogger& operator=(TelemetryTraceLogger&&) = default; - // Gets the singleton instance of this type. - static TelemetryTraceLogger& GetInstance(); - // Control whether this trace logger is enabled at runtime. bool DisableRuntime(); void EnableRuntime(); + // Return address of m_activityId + const GUID* GetActivityId() const; + + // Capture if UserSettings is enabled and set user profile path + void Initialize(); + // Store the passed in name of the Caller for COM calls void SetCaller(const std::string& caller); @@ -123,8 +129,6 @@ namespace AppInstaller::Logging void LogNonFatalDOError(std::string_view url, HRESULT hr) const noexcept; protected: - TelemetryTraceLogger(); - bool IsTelemetryEnabled() const noexcept; // Used to anonymize a string to the best of our ability. @@ -135,6 +139,7 @@ namespace AppInstaller::Logging bool m_isSettingEnabled = true; std::atomic_bool m_isRuntimeEnabled{ true }; + GUID m_activityId = GUID_NULL; std::wstring m_telemetryCorrelationJsonW = L"{}"; std::string m_caller; @@ -142,17 +147,19 @@ namespace AppInstaller::Logging std::wstring m_userProfile; }; + struct GlobalTelemetryTraceLogger : TelemetryTraceLogger + { + GlobalTelemetryTraceLogger() { Initialize(); } + + ~GlobalTelemetryTraceLogger() = default; + }; + // Helper to make the call sites look clean. TelemetryTraceLogger& Telemetry(); // Turns on wil failure telemetry and logging. void EnableWilFailureTelemetry(); - const GUID* GetActivityId(bool isNewActivity); - - // Set ActivityId - void SetActivityId(); - // An RAII object to disable telemetry during its lifetime. // Primarily used by the complete command to prevent messy input from spamming us. struct DisableTelemetryScope diff --git a/src/AppInstallerCommonCore/Public/winget/ThreadGlobals.h b/src/AppInstallerCommonCore/Public/winget/ThreadGlobals.h @@ -0,0 +1,48 @@ +#pragma once + +#include <AppInstallerLogging.h> +#include <AppInstallerTelemetry.h> +#include <mutex> + +namespace AppInstaller::ThreadLocalStorage +{ + + struct PreviousThreadGlobals; + + struct ThreadGlobals + { + ThreadGlobals() = default; + ~ThreadGlobals() = default; + + AppInstaller::Logging::DiagnosticLogger& GetDiagnosticLogger(); + + AppInstaller::Logging::TelemetryTraceLogger& GetTelemetryLogger(); + + // Set Globals for Current Thread + // Return RAII object with it's ownership to set the AppInstaller ThreadLocalStorage back to previous state + std::unique_ptr<AppInstaller::ThreadLocalStorage::PreviousThreadGlobals> SetForCurrentThread(); + + // Return Globals for Current Thread + static ThreadGlobals* GetForCurrentThread(); + + private: + + void Initialize(); + + std::unique_ptr<AppInstaller::Logging::DiagnosticLogger> m_pDiagnosticLogger; + std::unique_ptr<AppInstaller::Logging::TelemetryTraceLogger> m_pTelemetryLogger; + std::once_flag loggerInitOnceFlag; + + }; + + struct PreviousThreadGlobals + { + ~PreviousThreadGlobals(); + + PreviousThreadGlobals(ThreadGlobals* previous) : m_previous(previous) {}; + + private: + + ThreadGlobals* m_previous; + }; +} diff --git a/src/AppInstallerCommonCore/Telemetry/TraceLogging.cpp b/src/AppInstallerCommonCore/Telemetry/TraceLogging.cpp @@ -15,6 +15,15 @@ bool g_IsTelemetryProviderEnabled{}; UCHAR g_TelemetryProviderLevel{}; ULONGLONG g_TelemetryProviderMatchAnyKeyword{}; +struct TraceProvider +{ + TraceProvider(); + + ~TraceProvider(); +}; + +TraceProvider g_TraceProvider{}; + void WINAPI TelemetryProviderEnabledCallback( _In_ LPCGUID /*sourceId*/, _In_ ULONG isEnabled, @@ -29,12 +38,12 @@ void WINAPI TelemetryProviderEnabledCallback( g_TelemetryProviderMatchAnyKeyword = matchAnyKeyword; } -void RegisterTraceLogging() +TraceProvider::TraceProvider() { TraceLoggingRegisterEx(g_hTraceProvider, TelemetryProviderEnabledCallback, nullptr); } -void UnRegisterTraceLogging() +TraceProvider::~TraceProvider() { TraceLoggingUnregister(g_hTraceProvider); } diff --git a/src/AppInstallerCommonCore/Telemetry/TraceLogging.h b/src/AppInstallerCommonCore/Telemetry/TraceLogging.h @@ -63,7 +63,4 @@ TRACELOGGING_DECLARE_PROVIDER(g_hTraceProvider); extern bool g_IsTelemetryProviderEnabled; extern UCHAR g_TelemetryProviderLevel; -extern ULONGLONG g_TelemetryProviderMatchAnyKeyword; - -extern void RegisterTraceLogging(); -extern void UnRegisterTraceLogging(); +extern ULONGLONG g_TelemetryProviderMatchAnyKeyword;+ \ No newline at end of file diff --git a/src/AppInstallerCommonCore/ThreadGlobals.cpp b/src/AppInstallerCommonCore/ThreadGlobals.cpp @@ -0,0 +1,74 @@ +#include "pch.h" +#include "Public/winget/ThreadGlobals.h" + +namespace AppInstaller::ThreadLocalStorage +{ + using namespace AppInstaller::Logging; + + // Set and return Globals for Current Thread + static ThreadGlobals* SetOrGetThreadGlobals(bool setThreadGlobals, ThreadGlobals* pThreadGlobals = nullptr); + + DiagnosticLogger& ThreadGlobals::GetDiagnosticLogger() + { + return *(m_pDiagnosticLogger); + } + + TelemetryTraceLogger& ThreadGlobals::GetTelemetryLogger() + { + return *(m_pTelemetryLogger); + } + + std::unique_ptr<PreviousThreadGlobals> ThreadGlobals::SetForCurrentThread() + { + Initialize(); + + std::unique_ptr<PreviousThreadGlobals> p_prevThreadGlobals = std::make_unique<PreviousThreadGlobals>(SetOrGetThreadGlobals(true, this)); + + return p_prevThreadGlobals; + } + + void ThreadGlobals::Initialize() + { + try + { + std::call_once(loggerInitOnceFlag, [this]() + { + m_pDiagnosticLogger = std::make_unique<DiagnosticLogger>(); + m_pTelemetryLogger = std::make_unique<TelemetryTraceLogger>(); + + // The above make_unique for TelemetryTraceLogger will either create an object or will throw which is caught below. + m_pTelemetryLogger->Initialize(); + }); + } + catch (...) + { + // May throw std::system_error if any condition prevents calls to call_once from executing as specified + // May throw std::bad_alloc or any exception thrown by the constructor of TelemetryTraceLogger + // Loggers are best effort and shouldn't block core functionality. So eat up the exceptions here + } + } + + ThreadGlobals* ThreadGlobals::GetForCurrentThread() + { + return SetOrGetThreadGlobals(false); + } + + ThreadGlobals* SetOrGetThreadGlobals(bool setThreadGlobals, ThreadGlobals* pThreadGlobals) + { + thread_local AppInstaller::ThreadLocalStorage::ThreadGlobals* t_pThreadGlobals = nullptr; + + if (setThreadGlobals == true) + { + AppInstaller::ThreadLocalStorage::ThreadGlobals* previous_pThreadGlobals = t_pThreadGlobals; + t_pThreadGlobals = pThreadGlobals; + return previous_pThreadGlobals; + } + + return t_pThreadGlobals; + } + + PreviousThreadGlobals::~PreviousThreadGlobals() + { + std::ignore = SetOrGetThreadGlobals(true, m_previous); + } +} diff --git a/src/AppInstallerCommonCore/TraceLogger.cpp b/src/AppInstallerCommonCore/TraceLogger.cpp @@ -3,6 +3,7 @@ #include "pch.h" #include "Public/winget/TraceLogger.h" #include "Public/AppInstallerTelemetry.h" +#include "Public/winget/ThreadGlobals.h" namespace AppInstaller::Logging { @@ -14,7 +15,7 @@ namespace AppInstaller::Logging TraceLoggingWriteActivity(g_hTraceProvider, "Diagnostics", - AppInstaller::Logging::GetActivityId(false), + nullptr, // TODO: ActivityId of the Global and COMContext telemetry to be logged in future nullptr, TraceLoggingString(strstr.str().c_str(), "LogMessage")); } diff --git a/src/Microsoft.Management.Deployment/PackageManager.cpp b/src/Microsoft.Management.Deployment/PackageManager.cpp @@ -123,7 +123,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation } manifest.ApplyLocale(targetLocale); - ::AppInstaller::Logging::Telemetry().LogManifestFields(manifest.Id, manifest.DefaultLocalization.Get<::AppInstaller::Manifest::Localization::PackageName>(), manifest.Version); + context->GetThreadGlobals().GetTelemetryLogger().LogManifestFields(manifest.Id, manifest.DefaultLocalization.Get<::AppInstaller::Manifest::Localization::PackageName>(), manifest.Version); context->Add<::AppInstaller::CLI::Execution::Data::Manifest>(std::move(manifest)); context->Add<::AppInstaller::CLI::Execution::Data::PackageVersion>(std::move(internalPackageVersion)); @@ -271,7 +271,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation { std::unique_ptr<COMContext> context = std::make_unique<COMContext>(); hstring correlationData = (options) ? options.CorrelationData() : L""; - context->SetLoggerContext(correlationData, ::AppInstaller::Utility::ConvertToUTF8(callerProcessInfoString)); + context->SetContextLoggers(correlationData, ::AppInstaller::Utility::ConvertToUTF8(callerProcessInfoString)); // Convert the options to arguments for the installer. if (options)