Skip to content
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

Is there an example of using Common Logging with .Net Core 2.0? #153

Closed
spankr opened this issue Oct 5, 2017 · 2 comments
Closed

Is there an example of using Common Logging with .Net Core 2.0? #153

spankr opened this issue Oct 5, 2017 · 2 comments

Comments

@spankr
Copy link

spankr commented Oct 5, 2017

I spent a bit of time digging around, trying to find an example of a NETCore 2.0 app that used Common Logging 3.4.1 (with NLog) and couldn't find one.
In the end, I figured it out on my own through trial & error.
(I'm eagerly waiting for Common Logging 4.x!)

Is there an official example available (that I couldn't find)?

If not, here is what I had to do:

    class Program
    {
        // Must be done (just once!) before any loggers are instantiated
        private static void SetupLogging()
        {
            var props = new Common.Logging.Configuration.NameValueCollection
            {
                { "configType", "FILE" },
                { "configFile", "./nlog.config" }
            };
            LogManager.Adapter = new Common.Logging.NLog.NLogLoggerFactoryAdapter(props);
        }

        static void Main(string[] args)
        {
            SetupLogging();

            var logger = LogManager.GetLogger<Program>();

            logger.Info("Hello World.");
            Console.ReadKey();
        }
  }

The issue I had was that in netcore20, finding configuration files is a lot different. (nlog.config file has to be available in the executing folder in this snippet)

@Troncek
Copy link

Troncek commented Nov 13, 2017

Here's another example using appsettings.json and Microsoft.Extensions.Configuration instead of setting it up programmatically.

appsettings,json

{
  "LogConfiguration": {
    "factoryAdapter": {
      "type": "Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog4412",
      "arguments": {
        "configType": "FILE",
        "configFile": "~/nlog.config"
      }
    }
  }
}

Setup:

using Common.Logging;
using Common.Logging.Configuration;
using Microsoft.Extensions.Configuration;

IConfiguration config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .Build();

LogConfiguration logConfiguration = new LogConfiguration();
config.GetSection("LogConfiguration").Bind(logConfiguration);
LogManager.Configure(logConfiguration);

Usage:

private static readonly ILog log = LogManager.GetLogger(typeof(SomeClass));

or

private static readonly ILog log = LogManager.GetLogger<SomeClass>();

asjafjell pushed a commit to digipost/signature-api-client-dotnet that referenced this issue Jan 18, 2018
- Prosjektstruktur er nå klar, og de fleste tester kjører
- All logging er disablet fordi Common.Logging ikke er støttet
i netstandard2.0. Det kan se ut til at en hacky fiks på det er her
net-commons/common-logging#153. Det kommer nok
ikke til å bli støttet før i versjon 4.0, så kanskje ikke umiddelbart :(
asjafjell pushed a commit to digipost/signature-api-client-dotnet that referenced this issue Oct 17, 2018
- Prosjektstruktur er nå klar, og de fleste tester kjører
- All logging er disablet fordi Common.Logging ikke er støttet
i netstandard2.0. Det kan se ut til at en hacky fiks på det er her
net-commons/common-logging#153. Det kommer nok
ikke til å bli støttet før i versjon 4.0, så kanskje ikke umiddelbart :(
asjafjell pushed a commit to digipost/signature-api-client-dotnet that referenced this issue Oct 23, 2018
- Prosjektstruktur er nå klar, og de fleste tester kjører
- All logging er disablet fordi Common.Logging ikke er støttet
i netstandard2.0. Det kan se ut til at en hacky fiks på det er her
net-commons/common-logging#153. Det kommer nok
ikke til å bli støttet før i versjon 4.0, så kanskje ikke umiddelbart :(
@spankr
Copy link
Author

spankr commented Jul 19, 2019

Team was asking this question and a google search came back with this page. I like the response provided by @Troncek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants