Skip to main content

C# Tips

Haven't set up VS code yet?

See how to set up VS Code for Streamer.bot C# linting here: https://docs.streamer.bot/api/csharp/recipes/visual-studio-code

Recommendations

One of the ways to debug your C# code is to monitor the log while you run the action.

My Personal Recommendations
  • Prefix each log entry with something you can filter the logs by
    • I usually create my own private void Log methods that will prefix the log prefix
  • You can use System.Reflection to get the current method name so you can pinpoint which method the log occurred

Recommendation Example

I usually set up a log prefix so I can find the log easily when reading the log file. I do this by declaring a const string that I can reuse for log methods.

using System;

//-------------------------------------------------//
#if EXTERNAL_EDITOR
public class Example : CPHInlineBase
#else
public class CPHInline
#endif
//-------------------------------------------------//
{
const string EXTENSION_NAME = "Example Extension";
const string LOG_PREFIX = $"{EXTENSION_NAME} Log: ";

public bool Execute()
{
string methodName = $"{System.Reflection.MethodBase.GetCurrentMethod().Name}: "; // Used to append the method name when logging

LogInfo($"{methodName}Trying to get %targetUserName%");

// Break if %targetUserName% doesn't exist
if (!CPH.TryGetArg("targetUserName", out string targetUserName))
{
LogError($"{methodName}%targetUserName% doesn't exist!");
return false;
}

LogInfo($"{methodName}Found %targetUserName% contains '{targetUserName}'");
return true;
}

private void LogError(string errorMessage)
{
CPH.LogError($"{EXTENSION_NAME}: {errorMessage}");
}

private void LogInfo(string infoMessage)
{
CPH.LogInfo($"{LOG_PREFIX}{infoMessage}");
}

private void LogDebug(string debugMessage)
{
CPH.LogDebug($"{LOG_PREFIX}{debugMessage}");
}

private void LogVerbose(string verboseMessage)
{
CPH.LogVerbose($"{LOG_PREFIX}{verboseMessage}");
}
}

Reading the logs in real-time

There are different ways for you to read the log file. Usually you'll want to do this in real-time, which is known as file tailing. You can do it multiple ways. Here are some examples:


This script can be run with PowerShell and will automatically display the log as it changes.

# Get the most recent log file from today
$dateToday = (Get-Date).ToString('yyyyMMdd')
$logFilePaths = Get-ChildItem -Path '.' -Recurse -Filter "log_$([regex]::Escape($dateToday))*.log"

if ($logFilePaths) {
$logFile = $logFilePaths[0]
Get-Content -Path $logFile.FullName -Tail 50 -Wait | ForEach-Object {
if ($_ -match 'Log:') { Write-Host $_ -ForegroundColor Green }
elseif ($_ -match 'FTL]') { Write-Host $_ -ForegroundColor Red }
elseif ($_ -match 'ERR]') { Write-Host $_ -ForegroundColor Yellow }
elseif ($_ -match 'WRN]') { Write-Host $_ -ForegroundColor Magenta }
elseif ($_ -match 'VRB]') { Write-Host $_ -ForegroundColor DarkGray }
elseif ($_ -match 'DBG]') { Write-Host $_ -ForegroundColor Cyan }
elseif ($_ -match 'INF]') { Write-Host $_ -ForegroundColor Gray }
else { Write-Host $_ -ForegroundColor White }
}
Start-Sleep -Milliseconds 2000
} else {
Write-Host "No log file found from today"
}

Credits

  • Nate1280 for creating Streamer.bot
  • tawmae for the tips on adding color to the PowerShell log viewer.