Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#类索引器方法调用(日志)_C#_Logging_Methods_Delegates_Indexer - Fatal编程技术网

C#类索引器方法调用(日志)

C#类索引器方法调用(日志),c#,logging,methods,delegates,indexer,C#,Logging,Methods,Delegates,Indexer,我正在尝试实现自己的日志解决方案。一切都很好,除了一件事,那就是不想解决。 我有一个类(Log),它有方法记录到文件等。 我可以像Log.Debug(message,args)一样使用它,但这对我来说还不够 不幸的是,在C#中,我们无法使呼叫操作符过载,以便能够执行类似于日志(message,args)的操作。 因此,我在网上搜索并找到了关于索引器的信息。 我现在的想法是做如下事情: 日志[日志模式](消息,参数) 但我就是不能让它工作。我目前有委托、方法和索引器,如下所示: /// &

我正在尝试实现自己的日志解决方案。一切都很好,除了一件事,那就是不想解决。 我有一个类(Log),它有方法记录到文件等。 我可以像Log.Debug(message,args)一样使用它,但这对我来说还不够

不幸的是,在C#中,我们无法使呼叫操作符过载,以便能够执行类似于日志(message,args)的操作。 因此,我在网上搜索并找到了关于索引器的信息。 我现在的想法是做如下事情:

日志[日志模式](消息,参数)

但我就是不能让它工作。我目前有委托、方法和索引器,如下所示:

    /// <summary>
    /// Delegate for logging function, used by the indexer.
    /// </summary>
    /// <param name="mode">The logging mode.</param>
    /// <param name="message">The message to log.</param>
    /// <param name="args">The args for the message (String.Format).</param>
    public delegate void LogDelegate(string mode, string message, params object[] args);

    public LogDelegate this[string mode]
    {
        get
        {
            return LogIndexer;
        }
    }

    public void LogIndexer(string mode, string message, params object[] args)
    {
        lock (_Lock)
        {
            _queue.Enqueue(new LogEntry(String.Format(message, args), mode));
        }
    }
//
///用于记录函数的委托,由索引器使用。
/// 
///日志记录模式。
///要记录的消息。
///消息的参数(String.Format)。
公共委托void LogDelegate(字符串模式、字符串消息、参数对象[]args);
公共日志委托此[字符串模式]
{
收到
{
返回登录器;
}
}
public void LogIndexer(字符串模式、字符串消息、参数对象[]args)
{
锁
{
_Enqueue(新的日志条目(String.Format(message,args),mode));
}
}
现在我的问题是,如何将索引器(mode)的一个参数传递给函数,以便像这样调用它:


日志“调试”

此[字符串模式]
getter应使用其
模式
参数以此模式返回lambda:

public delegate void LogDelegate(string message, params object[] args);

public LogDelegate this[string mode]
{
    get
    {
        return (message, args) => LogIndexer(mode, message, args); 
    }
}

Log.Debug()
有什么问题吗?这不是否定的,但如果我是你,我会马上停下来看看或frameworks.Log.Debug很好,但为什么不尝试更多呢?这整件事只是为了学习,它不会被用于生产代码中。