3

In Java, it's easy to invoke a default Logger object with minimal ceremony and zero configuration:

import java.util.logging.*;
public class Whatever {
    private static final Logger log = Logger.getLogger("com.foo.Whatever");
    public void doSomething() {
        log.debug("doing something");
    }
}

Assuming C# has a comparable equivalent (a default, console-outputting ILogger that can be easily invoked from a console application with minimal ceremony, doesn't require third-party frameworks, and is basically just a thin wrapper around Console.WriteLine()), what is it?

Alternatively, if there isn't a comparable default equivalent, can someone please show a simple example of how to do it in a C# console app?

My question SPECIFICALLY refers to ILogger.

There's a specific reason why I want to use ILogger -- my library will eventually be used by a Unity3d application. I don't know how to do it (yet), but apparently there's some way to make UnityEngine's Debug.Log Logger look like an ILogger to non-Unity libraries.

Basically, I want to be able to reap the benefits of UnityEngine's Debug.Log when the library runs under Unity, without making it dependent upon UnityEngine's Debug.Log to be able to compile or run at all. Getting an ILogger to work in a Console app is just the first step on a much longer journey.

H H
  • 263,252
  • 30
  • 330
  • 514
Bitbang3r
  • 6,826
  • 5
  • 27
  • 40
  • There isn't in general. There is for asp.net core. However using a library (not framework) like NLog is quite straightforward, but of course has more to offer than just outputting to console... – ZorgoZ Jan 04 '19 at 21:02
  • I'd just use a common logger like log4net and if necessary an adapter interface to expose for dependency injection, given your comment about wanting the interface to be compatible with another logger. then I'd just DI the logger and let them plug in whatever logger they want to use. It takes us like 2 minutes to pop on log4net in our apps. – Nikki9696 Jan 04 '19 at 21:33
  • Part of the problem is that I don't really know how to DO that yet in C#. I have plenty of experience with Java, Maven, and Gradle, but I'm still mostly stumbling blindly around Visual Studio. I've kind of developed an allergy to third-party c# libraries at this point, because I don't understand enough about the relationships and dependencies between Unity, Visual Studio, Mono, C#, and .NET to avoid having most of my attempts -- at best -- work when I run it in Unity under Windows, then blow up when I try building & running under Android. – Bitbang3r Jan 04 '19 at 21:47
  • I think your question may be about how to setup DI in a Core Console app. Google that. And then it's just about adding a nuget package. – H H Jan 04 '19 at 22:26

2 Answers2

3

The default is NullLogger. No need to implement your own.

See this article for more context.

Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
0

There is none, and I would not reference any third-party logger directly. Instead, define an interface for the logger that you want. It could be something as simple as

public interface ILogger
{
    Log(string message);
    LogException(Exception ex);
}

Use your own interface, not anyone else's. Then you can use any number of third-party loggers if you need one and adapt them to your interface. Or you could write an implementation that writes debug our console output.

It's great to use an existing library, just don't become coupled to it by injecting their interface all over the place in your classes.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • 3
    I would use the ILogger interface from Microsoft instead. Almost all third-party loggers can be hooked into that interface aswell. – maracuja-juice Mar 06 '19 at 09:43
  • @maracuja-juice I should take a second look. Microsoft has been creating separate packages for abstractions, so maybe my knee-jerk reaction doesn't fit in this case. – Scott Hannen Mar 06 '19 at 12:44
  • 3
    2.5 years later I don't feel the same as I did. Microsoft's ILogger is ubiquitous and I never create my own logging interface. – Scott Hannen Dec 06 '21 at 02:10