C# NLog未创建日志文件,通过代码进行配置

C# NLog未创建日志文件,通过代码进行配置,c#,.net,configuration,nlog,C#,.net,Configuration,Nlog,使用NLog v4.6.8 通过以下代码对其进行配置: public class Logger { public static void ConfigureLogger() { var config = new NLog.Config.LoggingConfiguration(); // target where to log to string logFileName = @"\log.txt";

使用NLog v4.6.8

通过以下代码对其进行配置:

public class Logger
{
    public static void ConfigureLogger()
    {
        var config = new NLog.Config.LoggingConfiguration();

        // target where to log to
        string logFileName = @"\log.txt";
        string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        var logfile = new NLog.Targets.FileTarget("logfile") { FileName = path + logFileName, KeepFileOpen = true, OpenFileCacheTimeout = 5 };

        // delete the log file, if it exists.
        string fullFilePath = path + logFileName;
        if (File.Exists(fullFilePath))
        {
            File.Delete(fullFilePath);
        }

        // rules for mapping loggers to targets
        // minimum and maximum log levels for logging targets
        config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);

        // apply config
        NLog.LogManager.Configuration = config;
    }

    // create an instance of the logger for each class
    public static NLog.Logger getLogger()
    {
        return NLog.LogManager.GetCurrentClassLogger();
    }

    // Flush and close down internal threads and timers.
    public static void flushLogger()
    {
        NLog.LogManager.Shutdown();
    }
}
Logger.Info("Doing something");
典型用法如下:

public class Logger
{
    public static void ConfigureLogger()
    {
        var config = new NLog.Config.LoggingConfiguration();

        // target where to log to
        string logFileName = @"\log.txt";
        string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        var logfile = new NLog.Targets.FileTarget("logfile") { FileName = path + logFileName, KeepFileOpen = true, OpenFileCacheTimeout = 5 };

        // delete the log file, if it exists.
        string fullFilePath = path + logFileName;
        if (File.Exists(fullFilePath))
        {
            File.Delete(fullFilePath);
        }

        // rules for mapping loggers to targets
        // minimum and maximum log levels for logging targets
        config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);

        // apply config
        NLog.LogManager.Configuration = config;
    }

    // create an instance of the logger for each class
    public static NLog.Logger getLogger()
    {
        return NLog.LogManager.GetCurrentClassLogger();
    }

    // Flush and close down internal threads and timers.
    public static void flushLogger()
    {
        NLog.LogManager.Shutdown();
    }
}
Logger.Info("Doing something");
程序集目录中没有日志文件。为什么会这样


找到的一些疑难解答建议表明,出现这种情况的一个常见原因是NLog的配置文件没有复制到输出目录。但是,项目或解决方案中没有配置文件。只有对所需DLL的引用。

乍一看,您的代码看起来是有效的。当然,请确保首先调用
Logger.ConfigureLogger

我认为这是一个写入权限错误。您可以尝试写入临时文件夹。还可以启用内部日志(从代码)


需要明确的是,不需要XML配置-这是可选的

安装NLog XML(配置)包:
安装包NLog.XML
@Li JyuGao它是否需要相同的版本,并且必须设置为复制到输出目录?@Li JyuGao在执行了我在上述评论中描述的操作后,仍然没有日志文件。是的,同一版本。在项目解决方案中,config的属性应该设置为[始终复制到输出目录]。您不需要nlog.xml包。这是一个旧的第三方软件包,在这种情况下不推荐使用