在从 .NET Framework 迁移到 .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指令或程序集引用?)
ILogger
的定义位置。 - DaiILogger
类型,但它出现在Serilog
和Microsoft.Extensions.Logging
中(尽管两种ILogger
类型不兼容)-但是Serilog
和MEL
都适用于.NET Framework
和.NET Core
。我假设您不熟悉将项目更改为使用.NET Core
而不是.NET Framework
。您是要从头开始重新创建csproj
文件吗? - Daicsproj
文件从旧式样更改为新式样(packageReference
),然后在此基础上进行修改以针对.NET Core(或.NET Standard)。 - Daiusing Microsoft.Extensions.Logging;
。 - MarkusEgle