【问题标题】:How to fix CS1503 Argument 1: cannot convert from 'string' to 'System.Type'?如何修复 CS1503 参数 1:无法从“字符串”转换为“System.Type”?
【发布时间】:2023-03-30 01:48:01
【问题描述】:

我在.net框架类库项目中有这段代码,我想在.net标准类库项目中重用它。它按预期工作,但在 .net 标准项目中出现编译错误。

public FxLogger(string logger)
{
    ILog AppLogger = LogManager.GetLogger(logger);

错误:

CS1503  Argument 1: cannot convert from 'string' to 'System.Type'

两个应用程序中的 log4net 版本 2.0.8 我可以在LogManager 类中看到这些方法声明

public static ILog GetLogger(string repository, string name);
public static ILog GetLogger(Assembly repositoryAssembly, Type type);
public static ILog GetLogger(string repository, Type type);
public static ILog GetLogger(Type type);
public static ILog GetLogger(Assembly repositoryAssembly, string name);

【问题讨论】:

  • 唯一接受单个参数的重载是需要一个类型参数。
  • @MaxSzczurek 是的,我可以看到,但是我有一个字符串,我需要将其转换为Type,什么类型?以及如何?
  • 您希望LogManager.GetLogger("vivek nuna") 会发生什么?还是任何其他字符串值?
  • @AlexeiLevenkov 它是LogManager.GetLogger(string.Empty)
  • What to what type and how? 你能描述一下吗?正如@AlexeiLevenkov 所说,当你通过LogManager.GetLogger(string.Empty) 时你想要什么预期的行为

标签: c# log4net .net-standard log4net-configuration


【解决方案1】:

试试

 ILog AppLogger = LogManager.GetLogger(Assembly.GetCallingAssembly(),logger);

看起来.net框架方法被定义为

public static ILog GetLogger(string name)
{
    return GetLogger(Assembly.GetCallingAssembly(), name);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    相关资源
    最近更新 更多