Skip to content

Instantly share code, notes, and snippets.

@pbering
Last active August 13, 2016 10:18
Show Gist options
  • Save pbering/00012aefd44a22c9a31c7832f8446bc2 to your computer and use it in GitHub Desktop.
Save pbering/00012aefd44a22c9a31c7832f8446bc2 to your computer and use it in GitHub Desktop.
Sitecore native UDP log streaming

Sitecore native UDP log streaming

Add the config file to your local Sitecore instances and then run UDP.ps1, no more fiddling around with finding and reloading the current log file for each instance.

Enjoy :)

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<log4net>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender, Sitecore.Logging">
<remoteAddress value="127.0.0.1" />
<remotePort value="9000" />
<layout type="log4net.Layout.PatternLayout, Sitecore.Logging">
<conversionPattern value="%a @@@ %d{ABSOLUTE} @@@ %t @@@ %p @@@ %c @@@ %m" />
</layout>
</appender>
<root>
<priority value="INFO">
<patch:attribute name="value">DEBUG</patch:attribute>
</priority>
<appender-ref ref="UdpAppender" />
</root>
</log4net>
</sitecore>
</configuration>
CLS
$ErrorActionPreference = "STOP"
$splitString = " @@@ "
$port = 9000
$colors = @{"ERROR" = [ConsoleColor]::Red;
"FATAL" = [ConsoleColor]::Red;
"DEBUG" = [ConsoleColor]::White;
"WARN" = [ConsoleColor]::Yellow;
"INFO" = [ConsoleColor]::Gray; }
$levels = @{"ERROR" = "ERR";
"FATAL" = "FTL";
"DEBUG" = "DBG";
"WARN" = "WRN";
"INFO" = "NFO";}
try
{
$endpoint = New-Object Net.IPEndPoint ([IPAddress]::Any, $port)
$client = New-Object Net.Sockets.UdpClient $port
while($true)
{
$line = [Text.Encoding]::UTF8.GetString($client.Receive([ref]$endpoint))
if($line.IndexOf($splitString) -eq -1)
{
continue;
}
$segments = $line -split $splitString
$name = $segments[0]
try
{
$time = [DateTime]::Parse($segments[1])
}
catch
{
$time = [DateTime]::MinValue
}
$thread = $segments[2].Replace("Heartbeat", "HB").Replace("ManagedPoolThread #", "MPT#")
$level = $segments[3]
$logger = $segments[4]
$message = $segments[5]
$widestName = @{ $true = $name.Length; $false = $widestName }[$name.Length -gt $widestName]
$widestThread = @{ $true = $thread.Length; $false = $widestThread }[$thread.Length -gt $widestThread]
$color = @{ $true = [ConsoleColor]::Green; $false = $colors[$level] }[$level -eq "INFO" -and $message -match "AUDIT"]
Write-Host ("[{0, -$widestName}] " -f $name) -NoNewline -ForegroundColor Gray
Write-Host ("{0} " -f $time.ToLongTimeString()) -NoNewline -ForegroundColor Gray
Write-Host ("[{0}] " -f $levels[$level]) -NoNewline -ForegroundColor $color
Write-Host ("[{0, -$widestThread}] " -f $thread) -NoNewline -ForegroundColor $color
Write-Host ("{0}: " -f $logger) -NoNewline -ForegroundColor Gray
Write-Host ("{0} " -f $message) -NoNewline -ForegroundColor $color
Write-Host ""
}
}
finally
{
if($client -ne $null)
{
$client.Close()
$client = $null
}
}
@pbering
Copy link
Author

pbering commented Jul 20, 2016

TIP: You can write anything instead of %a (AppDomain ID) in the conversionPattern for the appender, for example the project name that the instance belongs to,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment