NLog和运行控制台应用程序作为一个过程

时间:2015-03-14 07:29:45

标签: f# nlog

我在运行我的控制台应用程序作为隐藏进程时遇到了nlog问题。日志条目未写入。

我在nlog.config文件中配置了两个目标(控制台,文件),如果我手动运行我的控制台应用程序,它们可以正常运行。

nlog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      globalThreshold="Debug"
      internalLogFile="c:\nlog.txt" internalLogLevel="Trace">

    <variable name="defaultLayout" value="${longdate} | ${level} | ${logger} | ${message}"/>
    <variable name="logFilename" value="logs/${shortdate}.Scheduler.log"/>
    <variable name="errorLogFilename" value="logs/${shortdate}.Scheduler.log"/>

    <targets async="false">
        <target xsi:type="Console" name="console" layout="${defaultLayout}"/>
        <target name="file" xsi:type="File" fileName="${logFilename}" layout="${defaultLayout}" />
        <target name="errorFile" xsi:type="File" fileName="${errorLogFilename}" layout="${defaultLayout}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="file,console" />
        <logger name="*" minlevel="Warn" writeTo="errorFile" />
    </rules>
</nlog>

我的代码负责启动控制台应用程序进程:

 let setupProcess (enviromentName) (parameter) = 
        let targetPath = getTargetPath enviromentName
        let executable = System.IO.Path.Combine(targetPath, executable)
        let startInfo = new ProcessStartInfo()
        startInfo.WindowStyle <- System.Diagnostics.ProcessWindowStyle.Normal
        startInfo.FileName <- executable
        startInfo.Arguments <- parameter
        startInfo.RedirectStandardOutput <- true
        startInfo.UseShellExecute <- false
        startInfo

并开始这个过程:

 [<Fact>]
    let ``Run in debug mode``() =
        createEnviroment "debugEnv"
        let setup = setupProcess "debugEnv" "debug"
        use process' = Process.Start(setup)
        process'.BeginOutputReadLine() |> ignore
        process'.OutputDataReceived.Add(fun(args) -> printf "%s\n" args.Data)
        process'.WaitForExit(10000) |> ignore
        process'.Kill()|> ignore

我的项目可以在这里找到:https://github.com/aph5nt/cronix

1 个答案:

答案 0 :(得分:1)

修好了!

<variable name="logFilename" value="${basedir}/logs/${shortdate}.log"/>

Windows服务的默认目录是C:\ Windows \ System32,所以我不得不将$ {basedir}添加到输出日志路径,所以现在它指向我的Windows服务安装时的位置。