Using SeriLog through a wrapper class

不打扰是莪最后的温柔 提交于 2019-12-24 05:35:50

问题


I have a solution with multiple .EXEs, WCF services, Windows Services, etc... I want to use SeriLog for logging in all of these but I don't want these projects to add SeriLog directly. I want them to depend upon a Frameword dll that has various things like email, configuration, etc... To that end I want to create a wrapper class in this Framework dll. All my projects can then just refer to this wrapper class (part of a dll they are already referring to). So, I want something like:

public class TheLogger : Serilog.ILogger
{
    private static readonly TheLogger instance = new TheLogger();

    /// <summary>
    /// Creates the singleton instance of TheLogger class.
    /// </summary>
    static TheLogger()
    {
        // Configure SeriLog
        var log = new LoggerConfiguration().WriteTo.Console().CreateLogger();
        Log.logger = log;

        // Need to point the instance to SeriLog's implementation ????
        instance = (TheLogger)log;

    }

    private TheLogger()
    {

    }

    /// <summary>
    /// Returns the singleton instance of TheLogger class.
    /// </summary>
    public static TheLogger Instance
    {
        get
        {
            return instance;
        }
    }
}

And then, I would want the other projects to use syntax like

TheLogger.Instance.Information("I am here");

But obviously, TheLogger doesn't have the implementation provided by the SeriLog's Log.logger static logger. And without the reference to SeriLog I can't refer to Log.logger. To summarize, the question is how do I leverage SeriLog's Log.logger but wrap it in my own class?

来源:https://stackoverflow.com/questions/26470951/using-serilog-through-a-wrapper-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!