-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NullReferenceException thrown inside Metrics Library #58
Comments
I couldn't reproduce the issue. Could you provide more info about targeting/installed .NET frameworks and compiler version on your machine? Also, a working solution with all proper settings would be very helpful. The problem is very strange because the code generated by the compiler is equivalent to the suggested fix: public static class Metric
{
...
[CompilerGenerated]
private static readonly MetricsContext <Internal>k__BackingField;
internal static MetricsContext Internal
{
get
{
return Metric.<Internal>k__BackingField;
}
}
static Metric()
{
Metric.log = LogProvider.GetCurrentClassLogger();
Metric.<Internal>k__BackingField = (MetricsContext) new DefaultMetricsContext("Metrics.NET");
...
}
...
} |
Target Framework: .NET Framework 4.5.2 With the fix the compiled code (viewed with dotPeek) is different, you get just this:
Looking at other issues on stack overflow e.g. http://stackoverflow.com/questions/7400438/why-this-static-constructor-is-not-getting-called, some of the answers display similar unexpected behaviour with respect to the order in which the the static constructor vs. accessing static members are called. It might be that an exception is thrown during Metric() and gets swallowed but this isn't caught in my debugger for some reason. |
Ok, I think I've figured it out. The problem is not with the order of static fields and constructor initialization. internal static readonly MetricsContext Internal;
static Metric()
{
Metric.log = LogProvider.GetCurrentClassLogger();
Metric.Internal = (MetricsContext) new DefaultMetricsContext("Metrics.NET");
...
} The difference in the current implementation is that the field
The problem you are experiencing is caused by JIT compiler deciding to run the type initializer of |
I'm getting this as well, when following the instructions on the "Getting Started" wiki page (v0.5.1 from nuget): using Metrics;
Metric.Config
.WithHttpEndpoint("http://localhost:1234/")
.WithAllCounters(); Is there any workaround for this that would let me run and evaluate the library while larger refactoring issues are considered? |
@bpowers you could try merging in this fix I did earlier: mcapodici@22f347e. I don't think this will be the fix they will eventually use, but it may get you over the hump for now. |
I didn't manage to reproduce the issue on my machine. Still, I have merged your request @mcapodici and I've added an empty static constructor to the |
@bpowers have you by any chance checked whether this fixed the problems you were having? I'd like to close the issue if this is the case. |
I found an exception from the Metrics library written to our logs. I also have a fix which I will create a pull request for soon.
To reproduce the problem, create a new console application and add the v of Metrics.NET using NuGet. Paste the following Program.cs, enable exception debugging then start debugging from Visual Studio:
There are other ways to reproduce i.e. it is not specific to using WithErrorHandler.
The stack trace is:
The cause is that in this line in the Metric class:
internal static readonly MetricsContext Internal { get; } = new DefaultMetricsContext("Metrics.NET");
The .NET framework surprisingly doesn't initialise the context object before the first read of the property.
The fix is to replace the property with a field:
internal static readonly MetricsContext Internal = new DefaultMetricsContext("Metrics.NET");
The text was updated successfully, but these errors were encountered: