Skip to content

Commit

Permalink
Initialize logging of global logger for inproc invocations (#4490)
Browse files Browse the repository at this point in the history
With Inproc invocation, logging was only enabled at context level
(during an actual package operation). This change initializes the global
logger for inproc invocations so package catalog, package search and
package correlation can be logged too.

Manually validated logs exist after the change.
  • Loading branch information
yao-msft committed May 17, 2024
1 parent e1ee49e commit 4bfc7cb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/AppInstallerCLICore/COMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ namespace AppInstaller::CLI::Execution
return m_correlationData;
}

void COMContext::SetLoggers()
void COMContext::SetLoggers(std::optional<AppInstaller::Logging::Channel> channel, std::optional<AppInstaller::Logging::Level> level)
{
Logging::Log().EnableChannel(Settings::User().Get<Settings::Setting::LoggingChannelPreference>());
Logging::Log().SetLevel(Settings::User().Get<Settings::Setting::LoggingLevelPreference>());
Logging::Log().EnableChannel(channel.has_value() ? channel.value() : Settings::User().Get<Settings::Setting::LoggingChannelPreference>());
Logging::Log().SetLevel(level.has_value() ? level.value() : Settings::User().Get<Settings::Setting::LoggingLevelPreference>());

// TODO: Log to file for COM API calls only when debugging in visual studio
Logging::FileLogger::Add(s_comLogFileNamePrefix);
Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/COMContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace AppInstaller::CLI::Execution

// Set Diagnostic and Telemetry loggers, Wil failure callback
// This should be called only once per COM Server instance
static void SetLoggers();
static void SetLoggers(std::optional<AppInstaller::Logging::Channel> channel = std::nullopt, std::optional<AppInstaller::Logging::Level> level = std::nullopt);

// Set COM call context for diagnostic and telemetry loggers
// This should be called for every COMContext object instance
Expand Down
13 changes: 13 additions & 0 deletions src/AppInstallerCLICore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,18 @@ namespace AppInstaller::CLI
#endif

AppInstaller::CLI::Execution::COMContext::SetLoggers();
}

void InProcInitialize()
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (Settings::User().Get<Settings::Setting::EnableSelfInitiatedMinidump>())
{
Debugging::EnableSelfInitiatedMinidump();
}
#endif

// Explicitly set default channel and level before user settings from PackageManagerSettings
AppInstaller::CLI::Execution::COMContext::SetLoggers(AppInstaller::Logging::Channel::Defaults, AppInstaller::Logging::Level::Info);
}
}
3 changes: 3 additions & 0 deletions src/AppInstallerCLICore/Public/AppInstallerCLICore.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ namespace AppInstaller::CLI

// Initializes the Windows Package Manager COM server.
void ServerInitialize();

// Initializations for InProc invocation.
void InProcInitialize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace winrt::Microsoft::Management::Deployment::implementation
[&]()
{
success = AppInstaller::Settings::TryInitializeCustomUserSettings(AppInstaller::Utility::ConvertToUTF8(settingsContent));
if (success)
{
AppInstaller::Logging::Log().EnableChannel(AppInstaller::Settings::User().Get<AppInstaller::Settings::Setting::LoggingChannelPreference>());
AppInstaller::Logging::Log().SetLevel(AppInstaller::Settings::User().Get<AppInstaller::Settings::Setting::LoggingLevelPreference>());
}
});
return success;
}
Expand Down
1 change: 1 addition & 0 deletions src/WindowsPackageManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extern "C"
WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerInProcModuleInitialize() try
{
::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::InProc>::Create();
AppInstaller::CLI::InProcInitialize();
return S_OK;
}
CATCH_RETURN();
Expand Down

0 comments on commit 4bfc7cb

Please sign in to comment.