|
/*
<File>
<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
<License see="prj:///Documentation/License.txt"/>
<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
<CreationDate>2007/11/27 12:44</CreationDate>
<LastSubmissionDate>$Date: $</LastSubmissionDate>
<Version>$Revision: $</Version>
</File>
*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security;
namespace DanielVaughan.Logging.Filters
{
/// <summary>
/// Restricts a log entry based on the log entries
/// <see cref="ILogEntry.MachineName"/> value,
/// as specified in the comma delimited <em>machines</em> attribute
/// of the filter element.
/// <seealso cref="Environment.MachineName"/>
/// </summary>
class MachineFilter : FilterBase
{
// bool trusted;
readonly List<string> machines = new List<string>();
/// <summary>
/// Gets or sets a set of machine names to be used by an <see cref="IFilter"/>.
/// </summary>
/// <value>The users.</value>
public IEnumerable<string> Machines
{
get
{
return machines;
}
}
public MachineFilter()
{
Init += EnvironmentUserFilter_Init;
}
void EnvironmentUserFilter_Init(object sender, FilterInitEventArgs e)
{
ArgumentValidator.AssertNotNull(e, "e");
ArgumentValidator.AssertNotNull(e.ConfigurationElement, "e.ConfigurationElement");
/* Reset state. */
machines.Clear();
/* TODO: put in util.*/
var machinesAttribute = e.ConfigurationElement.Attributes["Machines"];
if (machinesAttribute == null)
{
throw new ClientLoggingException("Machines attribute does not exists."); /* TODO: Make localizable resource. */
}
try
{
string machinesValue = machinesAttribute.Value;
var machineValues = machinesValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
machines.AddRange(machineValues);
}
catch (Exception ex)
{
throw new ClientLoggingException("Users attribute is invalid. Should be a comma seperated list of names.", ex); /* TODO: Make localizable resource. */
}
if (Action == FilterAction.Default)
{
Action = FilterAction.Allow;
}
if (Action != FilterAction.Allow && Action != FilterAction.Deny)
{
throw new ConfigurationErrorsException(InvalidActionMessage);
}
}
public override bool IsValid(LogEntryOrigin origin, IClientInfo info)
{
string machineName = info.MachineName;
foreach (string name in machines)
{
if (machineName != name)
{
continue;
}
if (Action == FilterAction.Deny)
{
return false;
}
if (Action == FilterAction.Allow)
{
return true;
}
}
return Action != FilterAction.Allow;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts