nicko 2005/06/15 08:57:29 Modified: examples/net/1.0/Appenders/SampleAppendersApp/cs/src App.config LoggingExample.cs SampleAppendersApp.csproj Added: examples/net/1.0/Appenders/SampleAppendersApp/cs/src/Appender AsyncAppender.cs PatternFileAppender.cs Log: Added AsyncAppender and PatternFileAppender Revision Changes Path 1.5 +14 -2 logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/App.config Index: App.config =================================================================== RCS file: /home/cvs/logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/App.config,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- App.config 13 Jun 2005 18:10:34 -0000 1.4 +++ App.config 15 Jun 2005 15:57:29 -0000 1.5 @@ -67,22 +67,34 @@ <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender> + <appender name="AsyncConsoleAppender" type="SampleAppendersApp.Appender.AsyncAppender, SampleAppendersApp"> + <appender-ref ref="ColoredConsoleAppender" /> + </appender> + <appender name="FastDbAppender" type="SampleAppendersApp.Appender.FastDbAppender, SampleAppendersApp"> <connectionString value="Persist Security Info=False;Integrated Security=false;server=ate;database=log4net_test;Connect Timeout=30;User ID=sa;Password=sa" /> </appender> + <appender name="PatternFileAppender" type="SampleAppendersApp.Appender.PatternFileAppender, SampleAppendersApp"> + <!-- File pattern to group files by date, then by session property --> + <file value="%date{yyyy-MM-dd}\%property{session}\output.log" /> + <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> + </appender> <!-- Setup the root category, add the appenders and set the default level --> <root> <level value="ALL" /> - - <appender-ref ref="MessageObjectExpanderAppender" /> + + <appender-ref ref="PatternFileAppender" /> <appender-ref ref="ConsoleAppender" /> + <appender-ref ref="MessageObjectExpanderAppender" /> <appender-ref ref="MessageBoxAppender" /> <appender-ref ref="FireEventAppender" /> <appender-ref ref="SimpleSmtpAppender" /> <appender-ref ref="MsmqAppender" /> + <!-- + <appender-ref ref="AsyncConsoleAppender" /> <appender-ref ref="FastDbAppender" /> --> </root> 1.4 +2 -0 logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/LoggingExample.cs Index: LoggingExample.cs =================================================================== RCS file: /home/cvs/logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/LoggingExample.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- LoggingExample.cs 17 Jan 2005 21:39:46 -0000 1.3 +++ LoggingExample.cs 15 Jun 2005 15:57:29 -0000 1.4 @@ -46,6 +46,8 @@ /// <param name="args">command line arguments</param> public static void Main(string[] args) { + log4net.ThreadContext.Properties["session"] = 21; + // Hookup the FireEventAppender event if (FireEventAppender.Instance != null) { 1.4 +10 -0 logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/SampleAppendersApp.csproj Index: SampleAppendersApp.csproj =================================================================== RCS file: /home/cvs/logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/SampleAppendersApp.csproj,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- SampleAppendersApp.csproj 13 Jun 2005 18:10:34 -0000 1.3 +++ SampleAppendersApp.csproj 15 Jun 2005 15:57:29 -0000 1.4 @@ -114,6 +114,11 @@ BuildAction = "Compile" /> <File + RelPath = "Appender\AsyncAppender.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "Appender\FastDbAppender.cs" SubType = "Code" BuildAction = "Compile" @@ -139,6 +144,11 @@ BuildAction = "Compile" /> <File + RelPath = "Appender\PatternFileAppender.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "Appender\SimpleSmtpAppender.cs" SubType = "Code" BuildAction = "Compile" 1.1 logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/Appender/AsyncAppender.cs Index: AsyncAppender.cs =================================================================== #region Copyright & License // // Copyright 2001-2005 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #endregion using System; using System.Threading; using log4net.Appender; using log4net.Core; using log4net.Util; namespace SampleAppendersApp.Appender { public sealed class AsyncAppender : IAppender, IOptionHandler, IAppenderAttachable { private string m_name; public string Name { get { return m_name; } set { m_name = value; } } public void ActivateOptions() { } public FixFlags Fix { get { return m_fixFlags; } set { m_fixFlags = value; } } public void Close() { // Remove all the attached appenders lock(this) { if (m_appenderAttachedImpl != null) { m_appenderAttachedImpl.RemoveAllAppenders(); } } } public void DoAppend(LoggingEvent loggingEvent) { loggingEvent.Fix = m_fixFlags; System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncAppend), loggingEvent); } private void AsyncAppend(object state) { LoggingEvent loggingEvent = state as LoggingEvent; if (m_appenderAttachedImpl != null && loggingEvent != null) { m_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvent); } } #region IAppenderAttachable Members public void AddAppender(IAppender newAppender) { if (newAppender == null) { throw new ArgumentNullException("newAppender"); } lock(this) { if (m_appenderAttachedImpl == null) { m_appenderAttachedImpl = new log4net.Util.AppenderAttachedImpl(); } m_appenderAttachedImpl.AddAppender(newAppender); } } public AppenderCollection Appenders { get { lock(this) { if (m_appenderAttachedImpl == null) { return AppenderCollection.EmptyCollection; } else { return m_appenderAttachedImpl.Appenders; } } } } public IAppender GetAppender(string name) { lock(this) { if (m_appenderAttachedImpl == null || name == null) { return null; } return m_appenderAttachedImpl.GetAppender(name); } } public void RemoveAllAppenders() { lock(this) { if (m_appenderAttachedImpl != null) { m_appenderAttachedImpl.RemoveAllAppenders(); m_appenderAttachedImpl = null; } } } public IAppender RemoveAppender(IAppender appender) { lock(this) { if (appender != null && m_appenderAttachedImpl != null) { return m_appenderAttachedImpl.RemoveAppender(appender); } } return null; } public IAppender RemoveAppender(string name) { lock(this) { if (name != null && m_appenderAttachedImpl != null) { return m_appenderAttachedImpl.RemoveAppender(name); } } return null; } #endregion private AppenderAttachedImpl m_appenderAttachedImpl; private FixFlags m_fixFlags = FixFlags.All; } } 1.1 logging-log4net/examples/net/1.0/Appenders/SampleAppendersApp/cs/src/Appender/PatternFileAppender.cs Index: PatternFileAppender.cs =================================================================== #region Copyright & License // // Copyright 2001-2005 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #endregion using System; using System.IO; using System.Text; using log4net.Appender; using log4net.Util; using log4net.Layout; using log4net.Core; namespace SampleAppendersApp.Appender { public class PatternFileAppender : AppenderSkeleton { public PatternFileAppender() { } public PatternLayout File { get { return m_filePattern; } set { m_filePattern = value; } } public Encoding Encoding { get { return m_encoding; } set { m_encoding = value; } } public SecurityContext SecurityContext { get { return m_securityContext; } set { m_securityContext = value; } } override public void ActivateOptions() { base.ActivateOptions(); if (m_securityContext == null) { m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this); } } override protected void Append(LoggingEvent loggingEvent) { try { // Render the file name StringWriter stringWriter = new StringWriter(); m_filePattern.Format(stringWriter, loggingEvent); string fileName = stringWriter.ToString(); fileName = SystemInfo.ConvertToFullPath(fileName); FileStream fileStream = null; using(m_securityContext.Impersonate(this)) { // Ensure that the directory structure exists string directoryFullName = Path.GetDirectoryName(fileName); // Only create the directory if it does not exist // doing this check here resolves some permissions failures if (!Directory.Exists(directoryFullName)) { Directory.CreateDirectory(directoryFullName); } // Open file stream while impersonating fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.Read); } if (fileStream != null) { using(StreamWriter streamWriter = new StreamWriter(fileStream, m_encoding)) { RenderLoggingEvent(streamWriter, loggingEvent); } fileStream.Close(); } } catch(Exception ex) { ErrorHandler.Error("Failed to append to file", ex); } } private PatternLayout m_filePattern = null; private Encoding m_encoding = Encoding.Default; private SecurityContext m_securityContext; } }