在.net核心中找不到ILogger类

问题描述 投票:0回答:1

从.netframework迁移到.net核心时,会生成此类,并且我在ILogger中发现错误,并且未找到Logtype。我尝试找到该类,但未找到。

namespace Namespace1
{

    public class FileLogger : ILogger
    {
        private static readonly string s_Path = "C:\\log.txt";
        public IObfuscationEngine ObfuscationEngine { get; private set; }

        public FileLogger()
        {
            this.ObfuscationEngine = new DefaultObfuscationEngine();

            Clear();
        }

        private static void Clear()
        {
            try
            {
                if (!File.Exists(s_Path))
                    File.Create(s_Path);
            }
            catch (Exception)
            {
                throw new Exception("Cannot write to file: " + s_Path);
            }

            // empty the file to avoid it growing
            System.IO.File.WriteAllText(s_Path, string.Empty);

            using (StreamWriter sw = new StreamWriter(s_Path, true))
            {
                // write many empty lines to clear the buffers
                for (int i = 0; i < 50; i++)
                {
                    sw.WriteLine("");
                }
            }
        }

        public void MethodInfo(
            LogType traceLevel,
            string traceMessage,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            using (StreamWriter sw = new StreamWriter(s_Path, true))
            {
                sw.WriteLine("{0} {1}: {2},{3}({4}) - {5}", 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    traceLevel,
                    memberName, 
                    Path.GetFileName(sourceFilePath),
                    sourceLineNumber,
                    traceMessage);
            }
        }

        public void ErrorInfo(
            string traceMessage,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            using (StreamWriter sw = new StreamWriter(s_Path, true))
            {
                sw.WriteLine("{0} {1}: {2},{3}({4}) - {5}",
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    "ERROR",
                    memberName,
                    Path.GetFileName(sourceFilePath),
                    sourceLineNumber,
                    traceMessage);
            }
        }
    }
}

第一个错误

错误CS0246,找不到类型或名称空间名称'ILogger'(您是否缺少using指令或程序集引用?)

第二个错误

错误CS0246,找不到类型或名称空间名称'LogType'(您是否缺少using指令或程序集引用?)

c# .net-core .net-framework-version
1个回答
0
投票

Microsoft.Extensions.Logging.ILogger .net核心必须实现Log,IsEnabeld和BeginScope方法:

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        throw new NotImplementedException();
    }

public bool IsEnabled(LogLevel logLevel)
    {
        throw new NotImplementedException();
    }

public IDisposable BeginScope<TState>(TState state)
    {
        throw new NotImplementedException();
    }

将日志记录代码移动到这些方法。

© www.soinside.com 2019 - 2024. All rights reserved.