Skip to content

Commit fd55193

Browse files
CopilotwaldekmastykarzCopilotgarrytrinder
authored
Fix extra logging in non-root commands (#1500)
* Initial plan * Fix extra logging in non-root commands by filtering plugin messages Co-authored-by: waldekmastykarz <[email protected]> * Refactor: Use IsRootCommand instead of listing subcommand names Co-authored-by: waldekmastykarz <[email protected]> * Handle jwt command separately: no console logging, other subcommands log Co-authored-by: waldekmastykarz <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Enhance logging for non-root subcommands by adjusting filter levels and adding console formatter * Fix subcommands to use simple logging with plugin messages filtered out Co-authored-by: waldekmastykarz <[email protected]> * Keep rich logging for subcommands, only suppress plugin messages Co-authored-by: waldekmastykarz <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: waldekmastykarz <[email protected]> Co-authored-by: Waldek Mastykarz <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Garry Trinder <[email protected]>
1 parent b3213ee commit fd55193

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ sealed class DevProxyCommand : RootCommand
4343

4444
private static bool _hasGlobalOptionsResolved;
4545
private static bool _isStdioCommandResolved;
46+
private static bool _isJwtCommandResolved;
47+
private static bool _isRootCommandResolved;
4648
private static bool _stdioLogFilePathResolved;
4749

4850
public static bool HasGlobalOptions
@@ -78,6 +80,47 @@ public static bool IsStdioCommand
7880
}
7981
}
8082

83+
public static bool IsJwtCommand
84+
{
85+
get
86+
{
87+
if (_isJwtCommandResolved)
88+
{
89+
return field;
90+
}
91+
92+
var args = Environment.GetCommandLineArgs();
93+
field = args.Length > 1 && string.Equals(args[1], "jwt", StringComparison.OrdinalIgnoreCase);
94+
_isJwtCommandResolved = true;
95+
return field;
96+
}
97+
}
98+
99+
/// <summary>
100+
/// Determines if the root command (proxy itself) is being invoked.
101+
/// Returns true when no subcommand is specified (only options or no args).
102+
/// A subcommand is detected when the first non-program argument doesn't start with '-'.
103+
/// </summary>
104+
public static bool IsRootCommand
105+
{
106+
get
107+
{
108+
if (_isRootCommandResolved)
109+
{
110+
return field;
111+
}
112+
113+
var args = Environment.GetCommandLineArgs();
114+
// Skip the first argument which is the program name
115+
// If there are no more arguments, it's the root command
116+
// If the first argument starts with '-', it's an option (root command)
117+
// Otherwise, it's a subcommand name
118+
field = args.Length <= 1 || args[1].StartsWith('-');
119+
_isRootCommandResolved = true;
120+
return field;
121+
}
122+
}
123+
81124
public static string StdioLogFilePath
82125
{
83126
get

DevProxy/Extensions/ILoggingBuilderExtensions.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,53 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
3939
return builder;
4040
}
4141

42+
// For jwt command, suppress all logging to console to avoid interfering with token output
43+
if (DevProxyCommand.IsJwtCommand)
44+
{
45+
_ = builder
46+
.ClearProviders()
47+
.SetMinimumLevel(LogLevel.None);
48+
return builder;
49+
}
50+
51+
// For root command (proxy itself), use rich logging
52+
if (DevProxyCommand.IsRootCommand)
53+
{
54+
_ = builder
55+
.AddFilter("Microsoft.Hosting.*", LogLevel.Error)
56+
.AddFilter("Microsoft.AspNetCore.*", LogLevel.Error)
57+
.AddFilter("Microsoft.Extensions.*", LogLevel.Error)
58+
.AddFilter("System.*", LogLevel.Error)
59+
// Only show plugin messages when no global options are set
60+
.AddFilter("DevProxy.Plugins.*", level =>
61+
level >= configuredLogLevel &&
62+
!DevProxyCommand.HasGlobalOptions)
63+
.AddConsole(options =>
64+
{
65+
options.FormatterName = ProxyConsoleFormatter.DefaultCategoryName;
66+
options.LogToStandardErrorThreshold = LogLevel.Warning;
67+
}
68+
)
69+
.AddConsoleFormatter<ProxyConsoleFormatter, ProxyConsoleFormatterOptions>(options =>
70+
{
71+
options.IncludeScopes = true;
72+
options.ShowSkipMessages = configuration.GetValue("showSkipMessages", true);
73+
options.ShowTimestamps = configuration.GetValue("showTimestamps", true);
74+
}
75+
)
76+
.AddRequestLogger()
77+
.SetMinimumLevel(configuredLogLevel);
78+
return builder;
79+
}
80+
81+
// For other subcommands (cert, config, outdated, msgraphdb), use rich logging
82+
// but with plugin messages filtered out
4283
_ = builder
4384
.AddFilter("Microsoft.Hosting.*", LogLevel.Error)
4485
.AddFilter("Microsoft.AspNetCore.*", LogLevel.Error)
4586
.AddFilter("Microsoft.Extensions.*", LogLevel.Error)
4687
.AddFilter("System.*", LogLevel.Error)
47-
// Only show plugin messages when no global options are set
48-
.AddFilter("DevProxy.Plugins.*", level =>
49-
level >= configuredLogLevel &&
50-
!DevProxyCommand.HasGlobalOptions)
88+
.AddFilter("DevProxy.Plugins.*", LogLevel.None)
5189
.AddConsole(options =>
5290
{
5391
options.FormatterName = ProxyConsoleFormatter.DefaultCategoryName;
@@ -61,7 +99,6 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
6199
options.ShowTimestamps = configuration.GetValue("showTimestamps", true);
62100
}
63101
)
64-
.AddRequestLogger()
65102
.SetMinimumLevel(configuredLogLevel);
66103

67104
return builder;

0 commit comments

Comments
 (0)