ILogger class is not found in .net core

Viewed 5155

while migrate from .netframework to .net core this class is generated and i found error in ILogger and Logtype is not found. I try to find this class but not found .

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);
            }
        }
    }
}

first error

Error CS0246 The type or namespace name 'ILogger' could not be found (are you missing a using directive or an assembly reference?)

second error

Error CS0246 The type or namespace name 'LogType' could not be found (are you missing a using directive or an assembly reference?)

2 Answers

I had the same problem, the solution for me was adding a reference to the following library in my project: Microsoft.Extensions.Logging.Abstractions

Microsoft.Extensions.Logging.ILogger .net core must implement Log,IsEnabeld and BeginScope method:

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();
    }

Move logging code to these methods.

Related