microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

Azure functions V2 throwing internal Application Insights errors #237

Closed yigitgursoy closed 5 years ago

yigitgursoy commented 5 years ago

I'm getting internal Application Insights errors when I use Microsoft.ApplicationInsights.NLogTarget nuget package in Azure functions.

Below is a list of errors I get:

image

This is my logger implementation:

public sealed class AzureFunctionLogger : ILogger
{
    private static ILogger _logger;
    private static readonly object LockObject = new object();

    public AzureFunctionLogger(string configFilePath)
    {
        if (_logger == null)
        {
            lock (LockObject)
            {
                var loggerFactory = new NLogLoggerFactory();
                loggerFactory.AddNLog();
                LogManager.Configuration = new XmlLoggingConfiguration($"{configFilePath}\\nlog.config");
                _logger = loggerFactory.CreateLogger("AzureFunctionLogger");
            }
        }
    }

    public IDisposable BeginScope<TState>(TState state)
    {
        return _logger.BeginScope(state);
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return _logger.IsEnabled(logLevel);
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        _logger.Log(logLevel, eventId, state, exception, formatter);
    }
}

public static class TestTrigger
{
    private static readonly ILogger Logger;

    static TestTrigger()
    {
        var executingAssembly = new FileInfo(Assembly.GetExecutingAssembly().Location);
        Logger = new AzureFunctionLogger(executingAssembly.Directory.Parent.FullName);
    }

    [FunctionName("TestTrigger")]
    public static async Task Run([EventHubTrigger("%...%", Connection = "...")]string[] eventHubMessages)
    {

...

And this is my NLog configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
    <extensions>
        <add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
    </extensions>
    <variable name="LogServiceName" value="test"/>
    <targets async ="true">
        <target xsi:type="ApplicationInsightsTarget" name="ai" InstrumentationKey="3b3aac08-1822-4a27-85c2-75932ca5e1bb" >
            <layout xsi:type="JsonLayout" renderEmptyObject="false">
                <attribute name="type" layout="appLog" />
                <attribute name="date" layout="${longdate:universalTime=true}Z" />
                <attribute name="version" layout="1.0.0.0" />

                <attribute name="payload" encode="false">
                    <layout type="JsonLayout" renderEmptyObject="false">
                        <attribute name="level" layout="${level:upperCase=true}" />
                        <attribute name="message" layout="${message}" />
                        <attribute name="exception" layout="${exception:format=type,tostring}" />
                        <attribute name="activity" layout="${activityid}" />

                        <attribute name="innerException" encode="false">
                            <layout type="JsonLayout" renderEmptyObject="false">
                                <attribute name="type" layout="${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=10:InnerExceptionSeparator=}" />
                                <attribute name="exception" layout="${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=10:InnerExceptionSeparator=}" />
                            </layout>
                        </attribute>
                        <attribute name="eventProperties" encode="false">
                            <layout type='JsonLayout' includeAllProperties="true" maxRecursionLimit="2" renderEmptyObject="false"/>
                        </attribute>
                    </layout>
                </attribute>
            </layout>
        </target>
    </targets>
    <rules async="true">
        <logger name="Microsoft.*" maxLevel="Trace" final="true" />
        <logger name="*" minlevel="Error" writeTo="ai" />
    </rules>
</nlog>
TimothyMothra commented 5 years ago

@lmolkova can you please check on this?

lmolkova commented 5 years ago

This is likely a duplicate of https://github.com/Microsoft/ApplicationInsights-dotnet/issues/973

Please try downgrading Application Insights 2.7.2 in your function project and it should go away.

I'm closing this as a duplicate in favor of https://github.com/Microsoft/ApplicationInsights-dotnet/issues/973.

yigitgursoy commented 5 years ago

Ok. Thanks @lmolkova