c# - 在 .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 指令或程序集引用?)
解决方案
我有同样的问题,我的解决方案是在我的项目中添加对以下库的引用:Microsoft.Extensions.Logging.Abstractions
推荐阅读
- printing - Netlogo 在同一行打印值和文本
- ios - XCTest:有没有办法在 UI 测试中通过 ID 查找任何类型的视图?
- r - 如何更改特定象限中点的颜色?
- java - 如何从 okhttp 调用中取回数据?
- c# - 我使用视图组件创建部分表单但模型绑定器无法绑定
- webpack - 一起使用 mini-css-extract-plugin 和 style-loader
- maven - TFS 作为本地 Maven 存储库
- java - Runnable 中的方法引用使对象 GC 安全吗?
- http - 如何不断将用户名和密码发送到特定 IP 地址(强制门户)?
- angular - 有没有办法在从服务器加载内容后检查元素的高度