在此过程中定义日志文件的位置

时间:2018-02-10 12:12:49

标签: c# log4net

我正在尝试创建日志文件而不事先定义其目标,但在运行时,如何做到这一点?这里MyApp.exe.log4net

<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <!-- <file type="log4net.Util.PatternString" value="%property{LogFileName}.txt"/>-->
      <file value="D:\Log.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="50MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
       <level value="ALL" />
       <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

这是我的日志初始化:

// init-log
string appName = Path.GetFileName(Assembly.GetExecutingAssembly().Location) + ".log4net";
string appPth = AppDomain.CurrentDomain.BaseDirectory;

CLog.LogInit(Path.Combine(appPth, appName));
CLog.WriteLog(LogLevelL4N.INFO, "(Program) MyApp application started.");

// init-log

这里是Lib.Clog课程:

public static class CLog
{
        public static void LogInit(string configFile, bool useAltLogFileNamePath = false, string altLogFileNamePath = "");
        public static void WriteDebugLog(string header, string subject, string detail, string note = "");
        public static void WriteExceptionLog(string header, Exception ex);
        public static void WriteInfoLog(string header, string subject, string detail, string note = "");
        public static void WriteLog(LogLevelL4N logLevel, string log);
}

现在日志文件总是转到D:\Log.log,但我希望该位置将在流程中定义

1 个答案:

答案 0 :(得分:0)

你试试这个:

XmlConfigurator.LogFullFilename = @"c:\path\Myapp.log";