如何使用C#windows服务应用程序中的nlog将数据记录到文件中 [英] How to log data to a file using nlog in C# windows service application

查看:354
本文介绍了如何使用C#windows服务应用程序中的nlog将数据记录到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我只是尝试使用c#windows service应用程序中的Nlog将数据记录到文件中,但问题是服务正常启动而没有创建其他文件来记录;我需要帮助 。 Nlog没有将数据记录到文本文件。



我尝试过:



 命名空间 NlogTestService 
{
public partial class NlogTestService:ServiceBase
{
public NlogTestService()
{
InitializeComponent();
}
Logger logger = LogManager.GetCurrentClassLogger();
受保护 覆盖 void OnStart ( string [] args)
{
logger.Debug( 服务开始);
线程thread1 = new 线程( new ThreadStart(NlogTestThread));
thread1.Start();
}

受保护 覆盖 void OnStop()
{
logger.Debug( 服务停了下来);
}
public void NlogTestThread()
{
logger.Debug( NlogTestThread开始);
}
}
}



-------------------- -------------------------------------------------- ---------

 <?  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

xsi:schemaLocation = http://www.nlog -project.org/schemas/NLog.xsd NLog.xsd

autoReload = true

throwExceptions = false

internalLogLevel = internalLogFile = c:\ temp\\\
log -internal.log
>

<! - 可选,添加一些变量
https://github.com/nlog/NLog/wiki/Configuration-file#variables
- >

< 变量 名称 = myvar value = myvalue / >

<! -
请参阅https://github.com/nlog/nlog/wiki / configuration-file
有关自定义日志记录规则和输出的信息。
- >

< 目标 >
< target name = f1 xsi:type = 文件 fileName = < span class =code-keyword> C:\ Users \Lbluser \Desktop \Projects\NlogTestService \NlogTestServiceLog.txt / >
<! - -
在此处添加目标
请参阅https://github.com/nlog/NLog/ wiki /目标可能的目标。
有关可能的布局渲染器,请参阅https://github.com/nlog/NLog/wiki/Layout-Renderers。
- >


< ! -
将事件写入文件名中包含日期的文件。
< target xsi:type =Filename =ffileName =$ {basedir} / logs / $ {shortdate} .log
layout =$ {longdate} $ {uppercase :$ {level}} $ {message}/>
- >



< 规则 >
< logger name = * < span class =code-attribute> minlevel = 调试 writeTo = f1 / >
<! - 添加您的记录规则他重新 - >

<! -
用最小级别的调试编写所有事件(所以调试,信息,警告,错误和致命,但不是跟踪)到f
< logger name =*minlevel =DebugwriteTo =f/>
- >

解决方案

{basedir} / logs /


{shortdate} .log
layout =


{longdate}

Hi,
I just tried to log data to a file using Nlog in c# windows service application, but the problem is service started normally and no other file get created to log ; i need help . Nlog is not logging data to text file.

What I have tried:

namespace NlogTestService
{
    public partial class NlogTestService : ServiceBase
    {
        public NlogTestService()
        {
            InitializeComponent();
        }
        Logger logger = LogManager.GetCurrentClassLogger();
        protected override void OnStart(string[] args)
        {
            logger.Debug("Service get started");
            Thread thread1 = new Thread(new ThreadStart(NlogTestThread));
            thread1.Start();
        }

        protected override void OnStop()
        {
            logger.Debug("Service got stopped");
        }
        public void NlogTestThread()
        {
            logger.Debug("NlogTestThread get started");
        }
    }
}


-------------------------------------------------------------------------------

<?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"

      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"

      autoReload="true"

      throwExceptions="false"

      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <target name ="f1" xsi:type="File" fileName="C:\Users\Lbluser\Desktop\Projects\NlogTestService\NlogTestServiceLog.txt"/>
    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  

  <rules>
    <logger name="*" minlevel="Debug" writeTo="f1" />
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->

解决方案

{basedir}/logs/


{shortdate}.log" layout="


{longdate}


这篇关于如何使用C#windows服务应用程序中的nlog将数据记录到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆