Skip to content
9 changes: 9 additions & 0 deletions DevProxy.Abstractions/Proxy/IProxyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public enum ReleaseType
Beta
}

public enum LogFor
{
[EnumMember(Value = "human")]
Human,
[EnumMember(Value = "machine")]
Machine
}

public interface IProxyConfiguration
{
int ApiPort { get; set; }
Expand All @@ -29,6 +37,7 @@ public interface IProxyConfiguration
IEnumerable<MockRequestHeader>? FilterByHeaders { get; }
bool InstallCert { get; set; }
string? IPAddress { get; set; }
LogFor LogFor { get; }
LogLevel LogLevel { get; }
ReleaseType NewVersionNotification { get; }
bool NoFirstRun { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion DevProxy.Plugins/SharePoint/CsomParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static (IEnumerable<string> Actions, IEnumerable<string> Errors) GetActio
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing XML: {ex.Message}");
errors.Add($"Error parsing XML: {ex.Message}");
}

return (actions, errors);
Expand Down
20 changes: 20 additions & 0 deletions DevProxy/Commands/DevProxyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sealed class DevProxyCommand : RootCommand
internal const string TimeoutOptionName = "--timeout";
internal const string DiscoverOptionName = "--discover";
internal const string EnvOptionName = "--env";
internal const string LogForOptionName = "--log-for";

private static readonly string[] globalOptions = ["--version"];
private static readonly string[] helpOptions = ["--help", "-h", "/h", "-?", "/?"];
Expand Down Expand Up @@ -396,6 +397,24 @@ private void ConfigureCommand()
}
});

var logForOption = new Option<LogFor?>(LogForOptionName)
{
Description = $"Target audience for log output. Allowed values: {string.Join(", ", Enum.GetNames<LogFor>())}",
HelpName = "log-for",
Recursive = true
};
logForOption.Validators.Add(input =>
{
if (input.Tokens.Count == 0)
{
return;
}
if (!Enum.TryParse<LogFor>(input.Tokens[0].Value, true, out _))
{
input.AddError($"{input.Tokens[0].Value} is not a valid log-for value. Allowed values are: {string.Join(", ", Enum.GetNames<LogFor>())}");
}
});

var options = new List<Option>
{
apiPortOption,
Expand All @@ -405,6 +424,7 @@ private void ConfigureCommand()
envOption,
installCertOption,
ipAddressOption,
logForOption,
logLevelOption,
noFirstRunOption,
portOption,
Expand Down
22 changes: 22 additions & 0 deletions DevProxy/Commands/DevProxyConfigOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DevProxy.Abstractions.Proxy;
using DevProxy.Abstractions.Utils;
using System.CommandLine;
using System.CommandLine.Parsing;
Expand All @@ -20,6 +21,7 @@ public string? ConfigFile
public int? ApiPort => _parseResult?.GetValueOrDefault<int?>(DevProxyCommand.ApiPortOptionName);
public bool Discover => _parseResult?.GetValueOrDefault<bool?>(DevProxyCommand.DiscoverOptionName) ?? false;
public string? IPAddress => _parseResult?.GetValueOrDefault<string?>(DevProxyCommand.IpAddressOptionName);
public LogFor? LogFor => _parseResult?.GetValueOrDefault<LogFor?>(DevProxyCommand.LogForOptionName);
public LogLevel? LogLevel => _parseResult?.GetValueOrDefault<LogLevel?>(DevProxyCommand.LogLevelOptionName);

public List<string>? UrlsToWatch
Expand Down Expand Up @@ -114,6 +116,25 @@ public DevProxyConfigOptions()
}
}
};

var logForOption = new Option<LogFor?>(DevProxyCommand.LogForOptionName)
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}

if (Enum.TryParse<LogFor>(result.Tokens[0].Value, true, out var logFor))
{
return logFor;
}

return null;
}
};

var apiPortOption = new Option<int?>(DevProxyCommand.ApiPortOptionName);

var discoverOption = new Option<bool>(DevProxyCommand.DiscoverOptionName, "--discover")
Expand All @@ -127,6 +148,7 @@ public DevProxyConfigOptions()
ipAddressOption,
configFileOption,
urlsToWatchOption,
logForOption,
logLevelOption,
discoverOption
};
Expand Down
55 changes: 41 additions & 14 deletions DevProxy/Extensions/ILoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DevProxy.Abstractions.Proxy;
using DevProxy.Commands;
using DevProxy.Logging;

Expand All @@ -26,6 +27,10 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
var configuredLogLevel = options.LogLevel ??
configuration.GetValue("logLevel", LogLevel.Information);

// Determine the log target audience (human or machine)
var configuredLogFor = options.LogFor ??
configuration.GetValue("logFor", LogFor.Human);

// For stdio command, log to file instead of console to avoid interfering with proxied streams
if (DevProxyCommand.IsStdioCommand)
{
Expand All @@ -48,6 +53,14 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
return builder;
}

var showSkipMessages = configuration.GetValue("showSkipMessages", true);
var showTimestamps = configuration.GetValue("showTimestamps", true);

// Select the appropriate formatter based on logFor setting
var formatterName = configuredLogFor == LogFor.Machine
? MachineConsoleFormatter.FormatterName
: ProxyConsoleFormatter.DefaultCategoryName;

// For root command (proxy itself), use rich logging
if (DevProxyCommand.IsRootCommand)
{
Expand All @@ -60,17 +73,24 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
.AddFilter("DevProxy.Plugins.*", level =>
level >= configuredLogLevel &&
!DevProxyCommand.HasGlobalOptions)
.AddConsole(options =>
.AddConsole(consoleOptions =>
{
options.FormatterName = ProxyConsoleFormatter.DefaultCategoryName;
options.LogToStandardErrorThreshold = LogLevel.Warning;
consoleOptions.FormatterName = formatterName;
consoleOptions.LogToStandardErrorThreshold = LogLevel.Warning;
}
)
.AddConsoleFormatter<ProxyConsoleFormatter, ProxyConsoleFormatterOptions>(options =>
.AddConsoleFormatter<ProxyConsoleFormatter, ProxyConsoleFormatterOptions>(formatterOptions =>
{
options.IncludeScopes = true;
options.ShowSkipMessages = configuration.GetValue("showSkipMessages", true);
options.ShowTimestamps = configuration.GetValue("showTimestamps", true);
formatterOptions.IncludeScopes = true;
formatterOptions.ShowSkipMessages = showSkipMessages;
formatterOptions.ShowTimestamps = showTimestamps;
}
)
.AddConsoleFormatter<MachineConsoleFormatter, ProxyConsoleFormatterOptions>(formatterOptions =>
{
formatterOptions.IncludeScopes = true;
formatterOptions.ShowSkipMessages = showSkipMessages;
formatterOptions.ShowTimestamps = showTimestamps;
}
)
.AddRequestLogger()
Expand All @@ -86,17 +106,24 @@ public static ILoggingBuilder ConfigureDevProxyLogging(
.AddFilter("Microsoft.Extensions.*", LogLevel.Error)
.AddFilter("System.*", LogLevel.Error)
.AddFilter("DevProxy.Plugins.*", LogLevel.None)
.AddConsole(options =>
.AddConsole(consoleOptions =>
{
consoleOptions.FormatterName = formatterName;
consoleOptions.LogToStandardErrorThreshold = LogLevel.Warning;
}
)
.AddConsoleFormatter<ProxyConsoleFormatter, ProxyConsoleFormatterOptions>(formatterOptions =>
{
options.FormatterName = ProxyConsoleFormatter.DefaultCategoryName;
options.LogToStandardErrorThreshold = LogLevel.Warning;
formatterOptions.IncludeScopes = true;
formatterOptions.ShowSkipMessages = showSkipMessages;
formatterOptions.ShowTimestamps = showTimestamps;
}
)
.AddConsoleFormatter<ProxyConsoleFormatter, ProxyConsoleFormatterOptions>(options =>
.AddConsoleFormatter<MachineConsoleFormatter, ProxyConsoleFormatterOptions>(formatterOptions =>
{
options.IncludeScopes = true;
options.ShowSkipMessages = configuration.GetValue("showSkipMessages", true);
options.ShowTimestamps = configuration.GetValue("showTimestamps", true);
formatterOptions.IncludeScopes = true;
formatterOptions.ShowSkipMessages = showSkipMessages;
formatterOptions.ShowTimestamps = showTimestamps;
}
)
.SetMinimumLevel(configuredLogLevel);
Expand Down
Loading