首页 > 解决方案 > 在 .net 核心中找不到 ILogger 类

问题描述

从 .netframework 迁移到 .net core 时,会生成此类,我在 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

解决方案


我有同样的问题,我的解决方案是在我的项目中添加对以下库的引用:Microsoft.Extensions.Logging.Abstractions


推荐阅读