Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call LogException(ex) rather than LogError(ex.ToString()) #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal static partial class ConsoleApp
}
else
{
LogError(ex.ToString());
LogException(ex);
}
}
}
Expand Down Expand Up @@ -678,7 +678,7 @@ await ConsoleApp.RunAsync(args, async Task<int> (string url, CancellationToken c
});
```

If the method throws an unhandled exception, ConsoleAppFramework always set `1` to the exit code. Also, in that case, output `Exception.ToString` to `ConsoleApp.LogError` (the default is `Console.WriteLine`). If you want to modify this code, please create a custom filter. For more details, refer to the [Filter](#filtermiddleware-pipline--consoleappcontext) section.
If the method throws an unhandled exception, ConsoleAppFramework always set `1` to the exit code. Also, in that case, output `Exception` to `ConsoleApp.LogException` (the default is `Console.WriteLine`). If you want to modify this code, please create a custom filter. For more details, refer to the [Filter](#filtermiddleware-pipline--consoleappcontext) section.

Attribute based parameters validation
---
Expand Down Expand Up @@ -974,7 +974,7 @@ ConsoleApp.ServiceProvider = serviceProvider;
ConsoleApp.Run(args, ([FromServices]MyService service, int x, int y) => Console.WriteLine(x + y));
```

`ConsoleApp` has replaceable default logging methods `ConsoleApp.Log` and `ConsoleApp.LogError` used for Help display and exception handling. If using `ILogger<T>`, it's better to replace these as well.
`ConsoleApp` has replaceable default logging methods `ConsoleApp.Log`, `ConsoleApp.LogError` and `ConsoleApp.LogException` used for Help display and exception handling. If using `ILogger<T>`, it's better to replace these as well.

```csharp
app.UseFilter<ReplaceLogFilter>();
Expand All @@ -987,13 +987,14 @@ internal sealed class ReplaceLogFilter(ConsoleAppFilter next, ILogger<Program> l
{
ConsoleApp.Log = msg => logger.LogInformation(msg);
ConsoleApp.LogError = msg => logger.LogError(msg);
ConsoleApp.LogException = ex => logger.LogCritical(ex, "Unhandled exception.");

return Next.InvokeAsync(context, cancellationToken);
}
}
```

> I don't recommend using `ConsoleApp.Log` and `ConsoleApp.LogError` directly as an application logging method, as they are intended to be used as output destinations for internal framework output.
> I don't recommend using `ConsoleApp.Log`, `ConsoleApp.LogError` and `ConsoleApp.LogException` directly as an application logging method, as they are intended to be used as output destinations for internal framework output.
> For error handling, it would be better to define your own custom filters for error handling, which would allow you to record more details when handling errors.

DI can also be effectively used when reading application configuration from `appsettings.json`. For example, suppose you have the following JSON file.
Expand Down
9 changes: 8 additions & 1 deletion src/ConsoleAppFramework/ConsoleAppBaseCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public static Action<string> LogError
set => logErrorAction = value;
}

static Action<Exception>? logExceptionAction;
public static Action<Exception> LogException
{
get => logExceptionAction ??= (static ex => LogError(ex.ToString()));
set => logExceptionAction = value;
}

/// <summary>
/// <para>You can pass second argument that generates new Run overload.</para>
/// ConsoleApp.Run(args, (int x, int y) => { });<br/>
Expand Down Expand Up @@ -386,7 +393,7 @@ static async Task RunWithFilterAsync(string commandName, string[] args, int comm
}
else
{
LogError(ex.ToString());
LogException(ex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleAppFramework/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
}
using (sb.BeginBlock("else"))
{
sb.AppendLine("LogError(ex.ToString());");
sb.AppendLine("LogException(ex);");
}
}
if (!emitForBuilder)
Expand Down