Author: wkeil Date: Sun Aug 23 10:51:11 2015 New Revision: 1697163 URL: http://svn.apache.org/r1697163 Log: DMAP-172: Build .NET clients with Visual Studio Code
Task-Url: https://issues.apache.org/jira/browse/DMAP-172 DMAP-152: Use Log4net to log in .NET clients Task-Url: https://issues.apache.org/jira/browse/DMAP-152 Added: devicemap/trunk/clients/1.0/csharp/log4net.dll (with props) Modified: devicemap/trunk/clients/1.0/csharp/DeviceMap/DeviceMap.csproj devicemap/trunk/clients/1.0/csharp/DeviceMap/util/Util.cs devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/App.config devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/DeviceMapConsole.csproj devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/Program.cs devicemap/trunk/clients/1.0/csharp/README Modified: devicemap/trunk/clients/1.0/csharp/DeviceMap/DeviceMap.csproj URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/DeviceMap/DeviceMap.csproj?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/DeviceMap/DeviceMap.csproj (original) +++ devicemap/trunk/clients/1.0/csharp/DeviceMap/DeviceMap.csproj Sun Aug 23 10:51:11 2015 @@ -41,6 +41,9 @@ </ApplicationIcon> </PropertyGroup> <ItemGroup> + <Reference Include="log4net"> + <HintPath>..\log4net.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.configuration" /> <Reference Include="System.Core" /> Modified: devicemap/trunk/clients/1.0/csharp/DeviceMap/util/Util.cs URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/DeviceMap/util/Util.cs?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/DeviceMap/util/Util.cs (original) +++ devicemap/trunk/clients/1.0/csharp/DeviceMap/util/Util.cs Sun Aug 23 10:51:11 2015 @@ -24,6 +24,12 @@ using System.Collections; using System.Diagnostics; using System.IO; using System.Text; +using log4net; +using log4net.Repository.Hierarchy; +using log4net.Core; +using log4net.Appender; +using log4net.Layout; + // namespace DeviceMap { @@ -123,27 +129,54 @@ namespace DeviceMap /// <param name="entryStr">String</param> /// <param name="type">EventLogEntryType</param> /// <remarks></remarks> - public static void WriteEntry(string appName, string entryStr, EventLogEntryType type = EventLogEntryType.Warning, Exception ex = null) - { - using (EventLog myLog = new EventLog()) - { - myLog.Source = appName; - if (!EventLog.SourceExists(myLog.Source)) - { - EventLog.CreateEventSource(myLog.Source, "Application"); - } - if (ex != null) - { - entryStr = string.Format("{0} : {1}", entryStr, ExceptionToString("ex", ex)); - } - byte[] btText = Encoding.UTF8.GetBytes(entryStr); - if (btText.Length > 32766) - { - // The message string is longer than 32766 bytes. - entryStr = BitConverter.ToString(btText, 0, 32760); - } - myLog.WriteEntry(entryStr, type); - } + /* public static void WriteEntry(string appName, string entryStr, EventLogEntryType type = EventLogEntryType.Warning, Exception ex = null) + { + using (EventLog myLog = new EventLog()) + { + myLog.Source = appName; + if (!EventLog.SourceExists(myLog.Source)) + { + EventLog.CreateEventSource(myLog.Source, "Application"); + } + if (ex != null) + { + entryStr = string.Format("{0} : {1}", entryStr, ExceptionToString("ex", ex)); + } + byte[] btText = Encoding.UTF8.GetBytes(entryStr); + if (btText.Length > 32766) + { + // The message string is longer than 32766 bytes. + entryStr = BitConverter.ToString(btText, 0, 32760); + } + myLog.WriteEntry(entryStr, type); + } + } */ + + public static void SetupLogging() + { + Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); + + PatternLayout patternLayout = new PatternLayout(); + patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; + patternLayout.ActivateOptions(); + + RollingFileAppender roller = new RollingFileAppender(); + roller.AppendToFile = true; + roller.File = @"DeviceMap.log"; + roller.Layout = patternLayout; + roller.MaxSizeRollBackups = 5; + roller.MaximumFileSize = "500MB"; + roller.RollingStyle = RollingFileAppender.RollingMode.Size; + roller.StaticLogFileName = true; + roller.ActivateOptions(); + hierarchy.Root.AddAppender(roller); + + MemoryAppender memory = new MemoryAppender(); + memory.ActivateOptions(); + hierarchy.Root.AddAppender(memory); + + hierarchy.Root.Level = Level.Info; + hierarchy.Configured = true; } } } \ No newline at end of file Modified: devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/App.config URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/App.config?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/App.config (original) +++ devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/App.config Sun Aug 23 10:51:11 2015 @@ -1,10 +1,7 @@ <?xml version="1.0"?> <configuration> <connectionStrings> - <!-- - <add name="_DeviceMap" connectionString="C:\Ddr\Resources"/> - <add name="_DeviceMap2" connectionString="http://svn.apache.org/repos/asf/incubator/devicemap/trunk/data/device-data/src/main/resources/devicedata"/> --> <!-- Deprecated --> - <add name="DeviceMap" connectionString ="http://devicemap-vm.apache.org/data/latest/"/> + <add name="DeviceMap" connectionString ="http://devicemap-vm.apache.org/data/latest/"/> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> Modified: devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/DeviceMapConsole.csproj URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/DeviceMapConsole.csproj?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/DeviceMapConsole.csproj (original) +++ devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/DeviceMapConsole.csproj Sun Aug 23 10:51:11 2015 @@ -70,6 +70,10 @@ <SignManifests>false</SignManifests> </PropertyGroup> <ItemGroup> + <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\log4net.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> Modified: devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/Program.cs URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/Program.cs?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/Program.cs (original) +++ devicemap/trunk/clients/1.0/csharp/DeviceMapConsole/Program.cs Sun Aug 23 10:51:11 2015 @@ -22,10 +22,15 @@ using System.Diagnostics; using System.IO; using System.Linq; using DeviceMap; +using log4net; class Program { + //Here is the once-per-class call to initialize the log object + private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + static void Main(string[] args) { + Util.SetupLogging(); string testDataDirectory = Util.Home(); string testDataFile = @"ua_strings.txt"; Stopwatch stopWatch = new Stopwatch(); @@ -38,50 +43,58 @@ class Program } catch (DeviceMapException deviceMapException) { - Util.WriteEntry("DeviceMap", deviceMapException.Message, EventLogEntryType.Error, deviceMapException); + //Util.WriteEntry("DeviceMap", deviceMapException.Message, EventLogEntryType.Error, deviceMapException); + log.Fatal("Client Error", deviceMapException); Console.WriteLine(deviceMapException.Message); } stopWatch.Stop(); Console.Clear(); Console.WriteLine("Loaded !"); - Console.WriteLine(string.Format("DeviceMap Client : {0}", client.Version)); - string str = client.DeviceCount.ToString(); - string str1 = client.PatternCount.ToString(); - double totalMilliseconds = stopWatch.Elapsed.TotalMilliseconds; - Console.WriteLine(string.Format("Loaded {0} devices with {1} patterns in {2} ms", str, str1, totalMilliseconds.ToString())); - stopWatch.Restart(); - Console.WriteLine("Cold run"); - Map(client, "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0"); - Map(client, "Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1"); - Map(client, "Mozilla/5.0 (BlackBerry; U; BlackBerry 9810; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.261 Mobile Safari/534.11+"); - Map(client, "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X; en-us) AppleWebKit/536.26 (KHTML, like Gecko) CriOS/23.0.1271.91 Mobile/10A403 Safari/8536.25"); - stopWatch.Stop(); - totalMilliseconds = stopWatch.Elapsed.TotalMilliseconds; - Console.WriteLine(string.Format("End cold run : {0} ms", totalMilliseconds.ToString())); - char[] chrArray = new char[] { '\\' }; - string data = string.Format("{0}\\{1}", testDataDirectory.TrimEnd(chrArray), testDataFile); - Console.WriteLine(string.Format("Press any key to run test file : {0}", data)); - Console.ReadKey(); - if (File.Exists(data)) + if (client != null) { - List<string> lines = new List<string>(File.ReadAllLines(data).ToList()); - List<string> cleanLines = (from l in lines select l into l where !string.IsNullOrWhiteSpace(l) select l).ToList<string>(); + Console.WriteLine(string.Format("DeviceMap Client : {0}", client.Version)); + string str = client.DeviceCount.ToString(); + string str1 = client.PatternCount.ToString(); + double totalMilliseconds = stopWatch.Elapsed.TotalMilliseconds; + Console.WriteLine(string.Format("Loaded {0} devices with {1} patterns in {2} ms", str, str1, totalMilliseconds.ToString())); stopWatch.Restart(); - int i = 0; - foreach (string ua in cleanLines) + Console.WriteLine("Cold run"); + Map(client, "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0"); + Map(client, "Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1"); + Map(client, "Mozilla/5.0 (BlackBerry; U; BlackBerry 9810; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.261 Mobile Safari/534.11+"); + Map(client, "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X; en-us) AppleWebKit/536.26 (KHTML, like Gecko) CriOS/23.0.1271.91 Mobile/10A403 Safari/8536.25"); + stopWatch.Stop(); + totalMilliseconds = stopWatch.Elapsed.TotalMilliseconds; + Console.WriteLine(string.Format("End cold run : {0} ms", totalMilliseconds.ToString())); + char[] chrArray = new char[] { '\\' }; + string data = string.Format("{0}\\{1}", testDataDirectory.TrimEnd(chrArray), testDataFile); + Console.WriteLine(string.Format("Press any key to run test file : {0}", data)); + Console.ReadKey(); + if (File.Exists(data)) { - Map(client, ua.Trim()); - i = i + 1; + List<string> lines = new List<string>(File.ReadAllLines(data).ToList()); + List<string> cleanLines = (from l in lines select l into l where !string.IsNullOrWhiteSpace(l) select l).ToList<string>(); + stopWatch.Restart(); + int i = 0; + foreach (string ua in cleanLines) + { + Map(client, ua.Trim()); + i = i + 1; + } + stopWatch.Stop(); + Console.WriteLine(string.Format("Tested {0} User-Agent strings in {1} ms.", i.ToString(), stopWatch.Elapsed.TotalMilliseconds.ToString())); } - stopWatch.Stop(); - Console.WriteLine(string.Format("Tested {0} User-Agent strings in {1} ms.", i.ToString(), stopWatch.Elapsed.TotalMilliseconds.ToString())); - } - else + else + { + Console.WriteLine(string.Format("Test file {0} not found.", data)); + } + Console.WriteLine("Press any key to finish"); + Console.ReadKey(); + } else { - Console.WriteLine(string.Format("Test file {0} not found.", data)); + log.Error("Client or Data not found"); + Console.WriteLine("DeviceMap Client or Data not found."); } - Console.WriteLine("Press any key to finish"); - Console.ReadKey(); } public static void Map(DeviceMapClient client, string text) Modified: devicemap/trunk/clients/1.0/csharp/README URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/README?rev=1697163&r1=1697162&r2=1697163&view=diff ============================================================================== --- devicemap/trunk/clients/1.0/csharp/README (original) +++ devicemap/trunk/clients/1.0/csharp/README Sun Aug 23 10:51:11 2015 @@ -15,4 +15,21 @@ To compile the sources you may use - Visual Studio (from VS 2010 upward, Community Edition is sufficient) - Mono Project (http://www.mono-project.com/) and MonoDevelop (http://www.monodevelop.com/) -We currently don't provide a MonoDevelop solution file, but the project layout should be compatible with it. And you're more than welcome to contribute solution files if you use MonDevelop (please create a JIRA ticket under https://issues.apache.org/jira/browse/DMAP/component/12323107 ".NET Client" and attach the required files) \ No newline at end of file +We currently don't provide a MonoDevelop solution, but the project layout should be compatible with it. And you're more than welcome to contribute solution files if you use MonDevelop (please create a JIRA ticket under https://issues.apache.org/jira/browse/DMAP/component/12323107 ".NET Client" and attach the required files) + +# Run + +## Configuration +DeviceMapConsole comes with a config file DeviceMapConsole.exe.config. +It contains the connection string for DeviceMap data: "http://devicemap-vm.apache.org/data/latest/" +This connection URL points to the latest available DeviceMap data version online. +If you prefer a different version or a local copy of the DeviceMap repository, you may override DeviceMapConsole by pointint to a different location. + +The config file also contains the supported .NET runtime version. +<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> +You should normally not have to change this. Doing so is at your own risk. DeviceMap C# client does not support .NET below 4.0, but in case you have multiple versions of the .NET Framework installed, you may chose a particular one, e.g. if you face problems running DeviceMapConsole. + +## Logging +DeviceMap uses Apache Log4Net (https://logging.apache.org/log4net/) +Regular operation of DeviceMapConsole only produces console output. +Logging is configured to log only errors or exceptional conditions into a file DeviceMap.log. Should this file exceed 500 MB, a RollingAppender will copy it over. See Apache Log4Net documentation for further details. \ No newline at end of file Added: devicemap/trunk/clients/1.0/csharp/log4net.dll URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/csharp/log4net.dll?rev=1697163&view=auto ============================================================================== Binary file - no diff available. Propchange: devicemap/trunk/clients/1.0/csharp/log4net.dll ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream
