Skip to content

Commit

Permalink
Fix AspNetFullFrameworkSampleApp logging (#508)
Browse files Browse the repository at this point in the history
* Add "Set up"/"ASP.NET" section

* Update "Supported Technologies"

* Add preCondition="managedHandler" part

* Fix AspNetFullFrameworkSampleApp logging

AspNetFullFrameworkSampleApp redirects agent's logging to NLog only if ELASTIC_APM_ASP_NET_FULL_FRAMEWORK_SAMPLE_APP_LOG_FILE environment variable is set. It is useful to have AspNetFullFrameworkSampleApp log to target other than file (Trace, etc.) when ELASTIC_APM_ASP_NET_FULL_FRAMEWORK_SAMPLE_APP_LOG_FILE is not set.

* Add "Elastic APM .NET AspNetFullFrameworkSampleApp>" prefix TraceTarget

Add "Elastic APM .NET AspNetFullFrameworkSampleApp>" prefix TraceTarget so it's easier to filter AspNetFullFrameworkSampleApp even the ones with multiple lines (exception stack traces, etc.)

* docs: fix config ids (#511)

[docs] Fix heading IDs: (1) revert configuration-on-asp-net-core-sample-config back to sample-config and (2) shorten configuration-on-asp-net-sample-config to asp-net-sample-config

* Add "Set up"/"ASP.NET" section

* Update "Supported Technologies"

* Add preCondition="managedHandler" part

* Apply suggestions from code review

Co-Authored-By: Brandon Morelli <[email protected]>

* Corrected ASP.NET version

* Fix AspNetFullFrameworkSampleApp logging

AspNetFullFrameworkSampleApp redirects agent's logging to NLog only if ELASTIC_APM_ASP_NET_FULL_FRAMEWORK_SAMPLE_APP_LOG_FILE environment variable is set. It is useful to have AspNetFullFrameworkSampleApp log to target other than file (Trace, etc.) when ELASTIC_APM_ASP_NET_FULL_FRAMEWORK_SAMPLE_APP_LOG_FILE is not set.

* Add "Elastic APM .NET AspNetFullFrameworkSampleApp>" prefix TraceTarget

Add "Elastic APM .NET AspNetFullFrameworkSampleApp>" prefix TraceTarget so it's easier to filter AspNetFullFrameworkSampleApp even the ones with multiple lines (exception stack traces, etc.)

* Code cleanup
  • Loading branch information
SergeyKleyman authored Sep 26, 2019
1 parent bb4d5d7 commit f8d0b70
Showing 1 changed file with 65 additions and 5 deletions.
70 changes: 65 additions & 5 deletions sample/AspNetFullFrameworkSampleApp/App_Start/LoggingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Elastic.Apm.AspNetFullFramework;
using NLog;
using NLog.Common;
Expand All @@ -20,7 +23,6 @@ public class LoggingConfig
public static void SetupLogging()
{
var logFileEnvVarValue = Environment.GetEnvironmentVariable(LogFileEnvVarName);
if (logFileEnvVarValue == null) return;

var config = new LoggingConfiguration();
const string layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss.fff zzz}" +
Expand All @@ -30,18 +32,22 @@ public static void SetupLogging()
" | ${message}" +
"${onexception:${newline}+-> Exception\\: ${exception:format=ToString}";

var logTargets = new TargetWithLayout[]
var logTargets = new List<TargetWithLayout>
{
new TraceTarget(), LogMemoryTarget, new FileTarget { FileName = logFileEnvVarValue, DeleteOldFileOnStartup = true },
new PrefixingTraceTarget($"Elastic APM .NET {nameof(AspNetFullFrameworkSampleApp)}> "),
LogMemoryTarget,
new ConsoleTarget()
};

if (logFileEnvVarValue != null) logTargets.Add(new FileTarget { FileName = logFileEnvVarValue, DeleteOldFileOnStartup = true });

foreach (var logTarget in logTargets) logTarget.Layout = layout;

// ReSharper disable once CoVariantArrayConversion
config.AddRule(LogLevel.Trace, LogLevel.Fatal, new SplitGroupTarget(logTargets));
config.AddRule(LogLevel.Trace, LogLevel.Fatal, new SplitGroupTarget(logTargets.ToArray()));

InternalLogger.LogToConsole = true;
InternalLogger.LogFile = logFileEnvVarValue;
if (logFileEnvVarValue != null) InternalLogger.LogFile = logFileEnvVarValue;
InternalLogger.LogLevel = LogLevel.Info;
InternalLogger.LogWriter = new StringWriter();

Expand All @@ -53,5 +59,59 @@ public static void SetupLogging()

Logger.Debug(nameof(SetupLogging) + " completed. Path to log file: {SampleAppLogFilePath}", logFileEnvVarValue);
}

private sealed class PrefixingTraceTarget : TargetWithLayout
{
// The order in endOfLines is important because we need to check longer sequences first
private static readonly string[] EndOfLineCharSequences = { "\r\n", "\n", "\r" };
private readonly string _prefix;

internal PrefixingTraceTarget(string prefix = "")
{
_prefix = prefix;
OptimizeBufferReuse = true;
}

protected override void Write(LogEventInfo logEvent)
{
var message = RenderLogEvent(Layout, logEvent);
Trace.WriteLine(PrefixEveryLine(message, _prefix));
}

private static string PrefixEveryLine(string input, string prefix = "")
{
// We treat empty input as a special case because StringReader doesn't return it as an empty line
if (input.Length == 0) return prefix;

var resultBuilder = new StringBuilder(input.Length);
using (var stringReader = new StringReader(input))
{
var isFirstLine = true;
string line;
while ((line = stringReader.ReadLine()) != null)
{
if (isFirstLine)
isFirstLine = false;
else
resultBuilder.AppendLine();
resultBuilder.Append(prefix);
resultBuilder.Append(line);
}
}

// Since lines returned by StringReader exclude newline characters it's possible that the last line had newline at the end
// but we didn't append it

foreach (var endOfLineSeq in EndOfLineCharSequences)
{
if (!input.EndsWith(endOfLineSeq)) continue;

resultBuilder.Append(endOfLineSeq);
break;
}

return resultBuilder.ToString();
}
}
}
}

0 comments on commit f8d0b70

Please sign in to comment.