In a previous post I showed how we configured a client’s application to use Apache log4net and Azure Application Insights.
I modified the code to allow the Instrumentation Key input via a command line parameter or from the ApplicationInsights.config file.
class Program { private static ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); static void Main(string[] args) { if (( args.Length != 0) && (args.Length != 1 )) { Console.WriteLine("Usage AzureApplicationInsightsClientConsole"); Console.WriteLine(" AzureApplicationInsightsClientConsole <instrumentationKey>"); return; } if (args.Length == 1) { TelemetryConfiguration.Active.InstrumentationKey = args[0]; } log.Debug("This is a Log4Net Debug message"); log.Info("This is a Log4Net Info message"); log.Warn("This is a Log4Net Warning message"); log.Error("This is an Log4Net Error message"); log.Fatal("This is a Log4Net Fatal message"); TelemetryConfiguration.Active.TelemetryChannel.Flush(); Console.WriteLine("Press <enter> to exit>"); Console.ReadLine(); } }
I updated the Log4Net setup to use the ManagedColoredConsoleAppender which required a couple of modifications to the Log4Net.config file. (I had to remove HighIntensity)
<appender name="ColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="White" /> <backColor value="Red" /> </mapping> <mapping> <level value="DEBUG" /> <backColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>
A sample project is available here.
Pingback: Apache Log4net .NET Core and Application Insights | devMobile's blog