スタートアップオブジェクトのMain()で一旦DisableLogging()を呼んでから、Settingsに従った初期化をする。
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); NLog.LogManager.DisableLogging(); Properties.Settings appsettings = Properties.Settings.Default; if (String.IsNullOrEmpty(appsettings.LogFolder)) { appsettings.LogFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + System.IO.Path.DirectorySeparatorChar + "Log"; } SetLocation(appsettings.LogFolder); if (appsettings.LogEnabled) { NLog.LogManager.EnableLogging(); } Application.Run(new MainForm()); } private static void SetLocation(string folder) { NLog.Config.LoggingConfiguration config = NLog.LogManager.Configuration; if (config != null) { ReadOnlyCollection<NLog.Targets.Target> targets = config.AllTargets; foreach (NLog.Targets.Target target in targets) { NLog.Targets.FileTarget ft = target as NLog.Targets.FileTarget; if (ft != null) { string filename = Path.GetFileName(ft.FileName.Render(NLog.LogEventInfo.CreateNullEvent())); ft.FileName = NLog.Layouts.Layout.FromString(folder + Path.DirectorySeparatorChar + filename); } } } }
0 件のコメント:
コメントを投稿