FileXmlLogger.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Security » Auditing » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Security » Auditing » FileXmlLogger.cs
using Exortech.NetReflector;
using System;
using System.IO;
using System.Text;
using System.Xml;
using ThoughtWorks.CruiseControl.Core.Util;
using ThoughtWorks.CruiseControl.Remote.Security;

namespace ThoughtWorks.CruiseControl.Core.Security.Auditing{
    /// <summary>
    /// Sends audit logging information to a file. The information will be stored in an XML format.
    /// </summary>
    /// <remarks>
    /// The actual file will not be correct XML as it will not have a single root element - instead each line will be directly written to the
    /// file.
    /// </remarks>
    /// <version>1.5</version>
    /// <title>XML File Audit Logger</title>
    /// <example>
    /// <code>
    /// &lt;xmlFileAudit location="c:\Logs\ccnet_audit.log"/&gt;
    /// </code>
    /// </example>
    [ReflectorType("xmlFileAudit")]
    public class FileXmlLogger
        : AuditLoggerBase, IAuditLogger
    {
        private string auditFile = "SecurityAudit.xml";
    private readonly IExecutionEnvironment executionEnvironment;

    public FileXmlLogger() : this(new ExecutionEnvironment())
    {}

    public FileXmlLogger(IExecutionEnvironment executionEnvironment)
    {
      this.executionEnvironment = executionEnvironment;
    }

        /// <summary>
        /// The location to log the audit events.
        /// </summary>
        /// <version>1.5</version>
        /// <default>SecurityAudit.xml</default>
        [ReflectorProperty("location", Required=false)]
        public string AuditFileLocation
        {
            get { return this.auditFile; }
            set { this.auditFile = value; }
        }

        /// <summary>
        /// Performs the actual logging of a security event
        /// </summary>
        /// <param name="projectName">The name of the project.</param>
        /// <param name="userName">The name of the user.</param>
        /// <param name="eventType">The type of event.</param>
        /// <param name="eventRight">The right of the event.</param>
        /// <param name="message">Any security message.</param>
        protected override void DoLogEvent(string projectName, string userName, SecurityEvent eventType, SecurityRight eventRight, string message)
        {
            // Generate the log entry
            XmlDocument auditXml = new XmlDocument();
            XmlElement xmlRoot = auditXml.CreateElement("event");
            auditXml.AppendChild(xmlRoot);
            AddXmlElement(auditXml, xmlRoot, "dateTime", DateTime.Now.ToString("o"));
            if (!string.IsNullOrEmpty(projectName)) AddXmlElement(auditXml, xmlRoot, "project", projectName);
            if (!string.IsNullOrEmpty(userName)) AddXmlElement(auditXml, xmlRoot, "user", userName);
            AddXmlElement(auditXml, xmlRoot, "type", eventType.ToString());
            if (eventRight != SecurityRight.Inherit) AddXmlElement(auditXml, xmlRoot, "outcome", eventRight.ToString());
            if (!string.IsNullOrEmpty(message)) AddXmlElement(auditXml, xmlRoot, "message", message);

            // Write the entry
      string auditLog = executionEnvironment.EnsurePathIsRooted(this.auditFile);
            lock (this)
            {
                File.AppendAllText(auditLog, auditXml.OuterXml.Replace(Environment.NewLine, " ") + Environment.NewLine);
            }
        }

        private void AddXmlElement(XmlDocument document, XmlElement parent, string name, string value)
        {
            XmlElement newNode = document.CreateElement(name);
            newNode.InnerText = value;
            parent.AppendChild(newNode);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.