/Dependencies/log4net/Util/PatternStringConverters/UserNamePatternConverter.cs
C# | 88 lines | 36 code | 7 blank | 45 comment | 5 complexity | f460325addce26a7235f2a110e21d76e MD5 | raw file
1#region Apache License 2// 3// Licensed to the Apache Software Foundation (ASF) under one or more 4// contributor license agreements. See the NOTICE file distributed with 5// this work for additional information regarding copyright ownership. 6// The ASF licenses this file to you under the Apache License, Version 2.0 7// (the "License"); you may not use this file except in compliance with 8// the License. You may obtain a copy of the License at 9// 10// http://www.apache.org/licenses/LICENSE-2.0 11// 12// Unless required by applicable law or agreed to in writing, software 13// distributed under the License is distributed on an "AS IS" BASIS, 14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15// See the License for the specific language governing permissions and 16// limitations under the License. 17// 18#endregion 19 20using System; 21using System.Text; 22using System.IO; 23 24using log4net.Util; 25 26namespace log4net.Util.PatternStringConverters 27{ 28 /// <summary> 29 /// Write the current threads username to the output 30 /// </summary> 31 /// <remarks> 32 /// <para> 33 /// Write the current threads username to the output writer 34 /// </para> 35 /// </remarks> 36 /// <author>Nicko Cadell</author> 37 internal sealed class UserNamePatternConverter : PatternConverter 38 { 39 /// <summary> 40 /// Write the current threads username to the output 41 /// </summary> 42 /// <param name="writer">the writer to write to</param> 43 /// <param name="state">null, state is not set</param> 44 /// <remarks> 45 /// <para> 46 /// Write the current threads username to the output <paramref name="writer"/>. 47 /// </para> 48 /// </remarks> 49 override protected void Convert(TextWriter writer, object state) 50 { 51#if (NETCF || SSCLI) 52 // On compact framework there's no notion of current Windows user 53 writer.Write( SystemInfo.NotAvailableText ); 54#else 55 try 56 { 57 System.Security.Principal.WindowsIdentity windowsIdentity = null; 58 windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); 59 if (windowsIdentity != null && windowsIdentity.Name != null) 60 { 61 writer.Write( windowsIdentity.Name ); 62 } 63 } 64 catch(System.Security.SecurityException) 65 { 66 // This security exception will occur if the caller does not have 67 // some undefined set of SecurityPermission flags. 68 LogLog.Debug(declaringType, "Security exception while trying to get current windows identity. Error Ignored."); 69 70 writer.Write( SystemInfo.NotAvailableText ); 71 } 72#endif 73 } 74 75 #region Private Static Fields 76 77 /// <summary> 78 /// The fully qualified type of the UserNamePatternConverter class. 79 /// </summary> 80 /// <remarks> 81 /// Used by the internal logger to record the Type of the 82 /// log message. 83 /// </remarks> 84 private readonly static Type declaringType = typeof(UserNamePatternConverter); 85 86 #endregion Private Static Fields 87 } 88}