C# configuration to log .NET library such socket connection

less than 1 minute read

If you create a file named [application].exe.config with the following contents in directory having your application, it can make logs on any socket connection and mail operations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<configuration>
    <system.diagnostics>
        <sources>
            <source name="System.Net">
                <listeners>
                    <add name="System.Net"/>
                </listeners>
            </source>
            <source name="System.Net.Sockets">
                <listeners>
                    <add name="System.Net"/>
                </listeners>
            </source>
	   <source name="System.Net.Mail">
	       <listeners>
	           <add name="System.Net"/>
	       </listeners>
	   </source>
        </sources>
        <switches>
            <add name="System.Net" value="Verbose" />
            <add name="System.Net.Sockets" value="Verbose" />
            <add name="System.Net.Mail" value="Verbose" />
        </switches>
        <sharedListeners>
            <add name="System.Net"
                type="System.Diagnostics.TextWriterTraceListener"
                initializeData="Application.log"
                />
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>
</configuration>