7 c# .net-framework-version .net-core
当从 .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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一个错误
错误 CS0246 找不到类型或命名空间名称“ILogger”(您是否缺少 using 指令或程序集引用?)
第二个错误
错误 CS0246 找不到类型或命名空间名称“LogType”(您是否缺少 using 指令或程序集引用?)
Microsoft.Extensions.Logging.ILogger .net core必须实现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();
}
Run Code Online (Sandbox Code Playgroud)
将日志记录代码移至这些方法。
| 归档时间: |
|
| 查看次数: |
10097 次 |
| 最近记录: |