Skip to content

Instantly share code, notes, and snippets.

@rgl
Created March 13, 2012 11:45
Show Gist options
  • Save rgl/2028339 to your computer and use it in GitHub Desktop.
Save rgl/2028339 to your computer and use it in GitHub Desktop.
Example on how to read application settings configuration
// NB you should probably use SixPack instead...
// On App.config:
/*
<configuration>
<configSections>
<section name="snmpPoller" type="Tracing.Service.Snmp.SnmpPollerSettings,Tracing.Service.Snmp"/>
</configSections>
<snmpPoller pollingInterval="4000" timeout="3000">
<variables>
<!--
sysUpTime OBJECT-TYPE (from SNMPv2-MIB)
SYNTAX TimeTicks
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"The time (in hundredths of a second) since the
network management portion of the system was last
re-initialized."
-->
<variable name="sysUpTime" oid="1.3.6.1.2.1.1.3.0"/>
</variables>
<nodes>
<node address="10.2.3.1" community="public" variables="sysUpTime"/>
<node address="10.2.3.2" community="public" variables="sysUpTime"/>
</nodes>
</snmpPoller>
*/
// On C# file (use as SnmpPollerSettings.Settings):
using System.Configuration;
namespace Tracing.Service.Snmp
{
public class SnmpPollerSettings : ConfigurationSection
{
public readonly static SnmpPollerSettings Settings = ConfigurationManager.GetSection("snmpPoller") as SnmpPollerSettings;
public class VariableElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)base["name"]; }
}
[ConfigurationProperty("oid", IsRequired = true)]
public string Oid
{
get { return (string)base["oid"]; }
}
}
public class VariablesCollection : ConfigurationElementCollection<string, VariableElement>
{
public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } }
protected override string ElementName { get { return "variable"; } }
protected override string GetElementKey(VariableElement element) { return element.Name; }
}
public class NodeElement : ConfigurationElement
{
[ConfigurationProperty("address", IsRequired = true)]
public string Address
{
get { return (string)base["address"]; }
}
[ConfigurationProperty("community", IsRequired = true)]
public string Community
{
get { return (string)base["community"]; }
}
[ConfigurationProperty("variables", IsRequired = true)]
public string Variables
{
get { return (string)base["variables"]; }
}
}
public class NodesCollection : ConfigurationElementCollection<string, NodeElement>
{
public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } }
protected override string ElementName { get { return "node"; } }
protected override string GetElementKey(NodeElement element) { return element.Address; }
}
[ConfigurationProperty("pollingInterval", IsRequired = true)]
public int PollingInterval
{
get { return (int)base["pollingInterval"]; }
}
[ConfigurationProperty("timeout", IsRequired = true)]
public int Timeout
{
get { return (int)base["timeout"]; }
}
[ConfigurationProperty("variables", IsRequired = true)]
public VariablesCollection Variables
{
get
{
return (VariablesCollection)base["variables"];
}
}
[ConfigurationProperty("nodes", IsRequired = true)]
public NodesCollection Nodes
{
get
{
return (NodesCollection)base["nodes"];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment