Skip to content

Commit

Permalink
🎇 Style: Enhanced
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Jun 18, 2024
1 parent 48bf463 commit ba3fdfe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion KitX Dashboard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void Main(string[] args)
}
catch (Exception e)
{
// Any unhandled exception will be catched here!
// Any unhandled exception will be caught here!
File.AppendAllText(
"./dump.log".GetFullPath(),
$"""
Expand Down
73 changes: 42 additions & 31 deletions KitX Dashboard/Services/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace KitX.Dashboard.Services;

public static class DebugService
{
private static readonly CSharpScriptEngine Engine = new();
private static CSharpScriptEngine Engine => new();

public static async Task<string?> ExecuteCodesAsync(string code, CancellationToken cancellationToken = default)
public static async Task<string?> ExecuteCodesAsync(
string code,
bool includeTimestamp = true,
CancellationToken cancellationToken = default
)
{
var sw = new Stopwatch();

Expand All @@ -24,43 +28,50 @@ public static class DebugService

try
{
var result = (await Engine.ExecuteAsync(
code,
options =>
{
options = options
.WithReferences(Assembly.GetExecutingAssembly())
.WithImports("KitX", "KitX.Dashboard")
.WithLanguageVersion(LanguageVersion.Preview)
;
var result = (
await Engine.ExecuteAsync(
code,
options =>
{
options = options
.WithReferences(Assembly.GetExecutingAssembly())
.WithImports("KitX", "KitX.Dashboard")
.WithLanguageVersion(LanguageVersion.Preview);
return options;
},
addDefaultImports: true,
runInReplMode: false,
cancellationToken: cancellationToken
))?.ToString();
return options;
},
addDefaultImports: true,
runInReplMode: false,
cancellationToken: cancellationToken
)
)?.ToString();

sw.Stop();

return new StringBuilder()
.AppendLine($"[{begin:yyyy-MM-dd HH:mm:ss}] [I] Posted.")
.AppendLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [I] Ended, took {sw.ElapsedMilliseconds} ms.")
.AppendLine(result)
.ToString()
;
return includeTimestamp
? new StringBuilder()
.AppendLine($"[{begin:yyyy-MM-dd HH:mm:ss}] [I] Posted.")
.AppendLine(
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [I] Ended, took {sw.ElapsedMilliseconds} ms."
)
.AppendLine(result)
.ToString()
: result;
}
catch (Exception e)
{
sw.Stop();

return new StringBuilder()
.AppendLine($"[{begin:yyyy-MM-dd HH:mm:ss}] [I] Posted.")
.AppendLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [E] Exception caught after {sw.ElapsedMilliseconds} ms, Message: {e.Message}")
.AppendLine(e.StackTrace)
.ToString()
;
};
return includeTimestamp
? new StringBuilder()
.AppendLine($"[{begin:yyyy-MM-dd HH:mm:ss}] [I] Posted.")
.AppendLine(
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [E] Exception caught after {sw.ElapsedMilliseconds} ms, Message: {e.Message}"
)
.AppendLine(e.StackTrace)
.ToString()
: e.StackTrace;
}
;
}
}

0 comments on commit ba3fdfe

Please sign in to comment.