NLog with Visual Studio Unit Test
|
We all have been writing unit tests for quite
some time. We all have also used NLog for logging
DEBUG's, ERRORS and TRACES in our web application
and web services. But after searching for a long
time to implement NLog in visual studio unit test
framework, I got no success. My test results we
still getting logged in test results.trx files
containing huge amount of data. In this post I am going to show you how we can use NLog to log our messages in log files same as we do in web applications and web services. For enabling logging in our unit test framework, open the app.config and include the following references: |
1: <configuration>
2:
3: <configSections>
4: <section name="nlog"
type="NLog.Config.ConfigSectionHandler, NLog"/>
5: </configSections>
6:
7: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
8: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
9:
10: <targets>
11: <target name="file"
12: xsi:type="File"
13: layout="${longdate} ${level:uppercase=true} ${message}"
14: fileName="C:/logs/${machinename}-applog.log"
15: archiveEvery="Day"
16: archiveFileName="C:/logs/applog.{#####}.log"
17: archiveNumbering="Sequence"
18: keepFileOpen="true"
19: encoding="iso-8859-2"
20: maxArchiveFiles="32"
21: />
22: </targets>
23: <rule>
24: <logger name="*" minlevel="Trace" writeTo="file"/>
25: </rule>
26:
27: </nlog>
28:
29: </configuration>
Now in the ClassInitize event, add the
following code to initialize the logger:
1: NLog.Logger logger = LogManager.GetLogger("UnitTestLogger");
2: logger.Trace("Logger Created");
Here you go. Follow the path C:\Logs to
view messages getting logged.
Happy
Programming!!!
ShareThis