7

I'm writing a WinForms application. I need to log information into a file. Usually I use log4net for logging, but I cannot add the reference, due to a restriction. I cannot add external references to my project, because I must deploy a single executable.

Is there any built-in logging framework in .NET so I will be able to log into a file without adding an external dll?

P.S: Of course I don't wanna open a stream and write manually.

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • If .NET framework shipped a full-blown logging framework there would be no need for log4net in the first place... – Alex Jul 10 '12 at 08:23
  • 1
    @Alex not all .NET functionality is brilliant, there are various external libraries that replace parts of the framework because they're doing a better job at it. – CodeCaster Jul 10 '12 at 08:24
  • @Daniel where does this requirement come from? Is [merging](http://stackoverflow.com/questions/39116/linking-statically-in-c-sharp) the assemblies an option? – CodeCaster Jul 10 '12 at 08:28
  • 1
    .NET does ship with a full blown logging framework, just one with a sensible amount of severity levels – Chris S Jul 10 '12 at 08:42

4 Answers4

12

Yes, the System.Diagnostics.TraceListener class. You will need to define the TRACE constant for it to work, but you can use a number of built in tracelisteners, through configuration of your app.config:

The app.config looks something like this if you want to write to a file, there are a lot of filters you can also add:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.diagnostics>
        <trace autoflush="false" indentsize="4">
          <listeners>
            <add name="yourName" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\mylogfile.txt" />
          </listeners>
        </trace>
    </system.diagnostics>
</configuration>

And usage:

Trace.TraceError("There's been an error captain: {0}", e);
Trace.TraceWarning("The system broke but don't worry.");
Trace.TraceInformation("Starting up the engines.");

Personally I wouldn't write to a text file if you can avoid it, the Event Log is a better location as you can sort, filter, the logs are auto-purged and you don't get file lock up issues.

Chris S
  • 64,770
  • 52
  • 221
  • 239
8

Well, if you're comfortable with log4net so be it. Use the logging tool of your choice, and in post-build use ILMerge to merge all dependencies into a single executable.

This is exactly the scenario that it is intended for.

There's also a GUI for it, if you don't want to use it in command line mode.

Asti
  • 12,447
  • 29
  • 38
2

You could use the system trace calls, but it's a poor substitute for a proper logging framework like log4net.

http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx

An alternative, and maybe more flexible long term, approach would be to use ILMerge to combine all your external DLLs into a single executable after you've compiled everything.

http://www.microsoft.com/en-us/download/details.aspx?id=17630

Or package everything as an MSI or clickonce package of course depending on your definition of "single executable".

Paolo
  • 22,188
  • 6
  • 42
  • 49
0

The following page shows an example of logging manually to an XML file. Looks reasonably simple.

c# Best Method to create a log file

Hope it helps.

Community
  • 1
  • 1
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47