Enterpise Library 4.1
I'm using enterprise library 4.1 and i wish to create different log files for different event开发者_JAVA百科 types.
eg.
Error.log for Error events, Warning.log for Warning events, how can accomplish this ?
thx
Ok, first at all, if you have installed the ent lib 4.1, you can use the "Enterprise Libreary Configuration" application to configure this. (I really recommend to use this application)
I will assume that you already know the basics of the Loggin Application Block. The first thing you will need to add to the loggingConfiguration will be the listeners for each file you want.
i.e.
<listeners>
<add name="Error Listener" fileName=".\error.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
rollFileExistsBehavior="Increment" rollInterval="Day" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="Timestamp" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add name="Warning Listener" fileName=".\Warning.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="Day" formatter="simple Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="Timestamp" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</listeners>
As you see, you have here two listeners, the first one named "Error Listener" witch write logs on the Error.log file and the other one named "Warning Listener" witch write logs on the Warning.log file.
The next step is add the Log Categories. i.e:
<categorySources>
<add switchValue="All" name="Error">
<listeners>
<add name="Error Listener" />
</listeners>
</add>
<add switchValue="All" name="Warning">
<listeners>
<add name="Warning Listener" />
</listeners>
</add>
</categorySources>
As you can see the categories have a "listeners" element where you can add listeners. Now you have the log categories mapped to the listeners.
All you have to do now is to call the Logger.Write method as:
Logger.Write("Message","Category");
精彩评论