logging fails with bufferingwrapper with stackOverflowException
See original GitHub issueI am trying to setup nlog logging with ElasticSearch target in a asp.net core application, during the startup it eventually crashes with: An unhandled exception of type ‘System.StackOverflowException’ occurred in System.Private.CoreLib.dll
The moment of failure seems to vary, doing a step-wise debug seems to delay the crash a little bit but it still crashes before the site loads. after disabling “just my code” the stack overflow appears to be occuring in ElasticSearch.NET which is being called by NLOG.
here is the top of the stack:
[Managed to Native Transition]
System.Private.CoreLib.dll!System.Reflection.RuntimeAssembly.GetModules(System.Runtime.CompilerServices.QCallAssembly assembly, bool loadIfNotFound, bool getResourceModules, System.Runtime.CompilerServices.ObjectHandleOnStack retModuleHandles) Unknown
System.Private.CoreLib.dll!System.Reflection.RuntimeAssembly.GetModulesInternal(bool loadIfNotFound, bool getResourceModules) Unknown
System.Private.CoreLib.dll!System.Reflection.RuntimeAssembly.DefinedTypes.get() Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase<System.Reflection.ConstructorInfo, Elasticsearch.Net.Utf8Json.Internal.ArrayBuffer<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerator<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo>>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
> Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase<System.Reflection.TypeInfo, Elasticsearch.Net.Utf8Json.Internal.ArrayBuffer<System.Reflection.TypeInfo>, System.Collections.Generic.IEnumerator<System.Reflection.TypeInfo>, System.Collections.Generic.IEnumerable<System.Reflection.TypeInfo>>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.Collections.Generic.IEnumerable<System.Reflection.TypeInfo> value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase<System.Reflection.ConstructorInfo, Elasticsearch.Net.Utf8Json.Internal.ArrayBuffer<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerator<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo>>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase<System.Reflection.TypeInfo, Elasticsearch.Net.Utf8Json.Internal.ArrayBuffer<System.Reflection.TypeInfo>, System.Collections.Generic.IEnumerator<System.Reflection.TypeInfo>, System.Collections.Generic.IEnumerable<System.Reflection.TypeInfo>>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.Collections.Generic.IEnumerable<System.Reflection.TypeInfo> value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
[Lightweight Function]
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Resolvers.DynamicMethodAnonymousFormatter<System.__Canon>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.__Canon value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
Elasticsearch.Net.dll!Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase<System.Reflection.ConstructorInfo, Elasticsearch.Net.Utf8Json.Internal.ArrayBuffer<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerator<System.Reflection.ConstructorInfo>, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo>>.Serialize(ref Elasticsearch.Net.Utf8Json.JsonWriter writer, System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> value, Elasticsearch.Net.Utf8Json.IJsonFormatterResolver formatterResolver) Unknown
here is my config:
<target xsi:type="BufferingWrapper" name="ElasticSearch"
flushTimeout="5000">
<target xsi:type="ElasticSearch"
requireAuth="true"
username="admin"
password="..."
includeAllProperties ="true">
</target>
</target>
here is my Program class:
public static void Main(string[] args)
{
var logFactory = NLogBuilder.ConfigureNLog("nlog.config");
var logger = logFactory.GetCurrentClassLogger();
var appName = Assembly.GetExecutingAssembly().GetName().Name;
// setup logging first!
ConfigureLogging(logFactory, appName);
try
{
// this is succesfully logged to ES
logger.Info().Message("{Action} {Stage} web host builder for {Application}.", "Initiating","Startup",appName).Write();
CreateWebHostBuilder(args).Build().Run();
logger.Info().Message("{Action} {Stage} web host builder for {Application}, Result: {Result}", "Initiating", "Startup", appName, "Success").Write();
}
catch (System.Exception ex)
{
logger.Fatal().Exception(ex).Message("{Action} {Stage} web host builder for {Application}, Result: {Result}", "Initiating", "Startup", appName, "Exception").Write();
throw;
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
// Add custom logging providers the same way. TO DO: create a wiki page
// Reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2
//.UseSynthiumLog()
//.UseSerilog()
.UseNLog(new NLogAspNetCoreOptions() { IncludeScopes = true })
.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "Core.ApplicationBase") // Inject startup configuration from ApplicationBase
.UseStartup<Startup>()
;
public class esConfig
{
public string Uri { get; set; }
public string User { get; set; }
public string Password { get; set; }
public bool IndexEnvSuffix { get; set; } = false;
public string Index { get; set; }
}
private static void ConfigureLogging(NLog.LogFactory logFactory, string appName)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true)
.Build();
var target = logFactory.Configuration.FindTargetByName("ElasticSearch") as BufferingTargetWrapper;
var esTarget = target.WrappedTarget as ElasticSearchTarget;
foreach (var setting in configuration.GetChildren())
{
logFactory.Configuration.Variables[setting.Key] = setting.Value;
}
var cfg = configuration.GetSection("ElasticConfiguration").Get<esConfig>();
if (cfg != null)
{
esTarget.Uri = cfg.Uri;
if (!string.IsNullOrEmpty(cfg.User))
esTarget.Username = cfg.User;
if (!string.IsNullOrEmpty(cfg.Password))
esTarget.Password = cfg.Password;
esTarget.RequireAuth = !string.IsNullOrEmpty(esTarget.Username) && !string.IsNullOrEmpty(esTarget.Password);
var indexName = cfg.Index ?? $"{appName.ToLower().Replace(".", "-")}";
if (cfg.IndexEnvSuffix)
indexName += $"-{environment?.ToLower().Replace(".", "-")}";
esTarget.Index = Layout.FromString(indexName+"-${date:format=yyyy.MM.dd}");
}
}
and in the startup i have this in configureappservice:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
NLog: Why BufferingWrapper does not seem to buffer log ...
BufferingWrapper is a throttle-mechanism for delaying (or even discarding) logevents. If performance is important then one should use <targets ...
Read more >StackOverflowException Class (System)
The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited.
Read more >Stack overflow exception while logging out.
Stack overflow exception while logging out. Clicking on logout after original http session destroyed, results to StackOverFlow.
Read more >The StackOverflowError in Java
Another interesting scenario that causes this error is if a class is being instantiated within the same class as an instance variable of...
Read more >StackOverflowException when deploying package
I cannot deploy any release from my project due to StackOverflowException. Here is task log message: == Failed: Step 2: Deploy Web Site ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Done v7.4.0
@markmcdowell That was super fast! Thank you very much