Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented LogElapsed class #25

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions CtcApi.Ods/OdsApi.Tests/Test.CtcApi.Ods.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,4 @@
<PostBuildEvent>if exist $(ProjectDir)ConnectionStrings.config xcopy /y $(ProjectDir)ConnectionStrings.config $(OutDir)
if exist $(ProjectDir)AppSettings.config xcopy /y $(ProjectDir)AppSettings.config $(OutDir)</PostBuildEvent>
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_BuildVersioningStyle="None.None.None.Increment" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_ConfigurationName="Release" BuildVersion_BuildAction="ReBuild" />
</VisualStudio>
</ProjectExtensions>
</Project>
32 changes: 26 additions & 6 deletions CtcApi/CtcApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,41 @@
<Reference Include="System.DirectoryServices.AccountManagement" />
<Reference Include="System.Web" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.Helpers">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Deployment">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.WebPages.Deployment.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WebMatrix.Data">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\WebMatrix.Data.dll</HintPath>
</Reference>
<Reference Include="WebMatrix.WebData">
<HintPath>..\packages\Mvc3Assemblies.0.1.0\lib\WebMatrix.WebData.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationContext.cs" />
<Compile Include="Config\CtcConfigBase.cs" />
<Compile Include="Diagnostics\LogElapsed.cs" />
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
Expand Down Expand Up @@ -134,9 +159,4 @@
<!-- Package the project -->
<Exec Command="NuGet.exe pack -Symbols -OutputDir &quot;$(PackageDir)&quot; -Properties Configuration=$(Configuration)" />
</Target>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_BuildVersioningStyle="None.None.None.Increment" BuildVersion_BuildAction="ReBuild" BuildVersion_ConfigurationName="Release" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
</VisualStudio>
</ProjectExtensions>
</Project>
125 changes: 125 additions & 0 deletions CtcApi/Diagnostics/LogElapsed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//Copyright (C) 2012 Bellevue College
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as
//published by the Free Software Foundation, either version 3 of the
//License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License and GNU General Public License along with this program.
//If not, see <http://www.gnu.org/licenses/>.
using System;
using Common.Logging;

namespace CtcApi.Diagnostics
{
/// <summary>
///
/// </summary>
public class LogElapsed : IDisposable
{
readonly DateTime _start = DateTime.Now;
readonly String _label = "";

readonly ILog _log = LogManager.GetCurrentClassLogger();
private readonly LogLevel _startLevel = LogLevel.Trace;
private readonly LogLevel _finishLevel = LogLevel.Trace;

/// <summary>
///
/// </summary>
public string TimeFormat
{
get
{
// TODO: Retrieve value from .config
return "hh:mm:ss.ffff tt";
}
}

/// <summary>
///
/// </summary>
/// <param name="label"></param>
/// <param name="startLevel"></param>
/// <param name="finishLevel"></param>
public LogElapsed(String label = "", LogLevel startLevel = LogLevel.Trace, LogLevel finishLevel = LogLevel.Trace)
{
string msgStart = "Starting TIMER:";
if (!string.IsNullOrWhiteSpace(label))
{
_label = label;
msgStart = String.Format("Starting TIMER for: {0}", _label);
}

_start = DateTime.Now;
string message = ConstructMessage(msgStart, _start);

_startLevel = startLevel;
_finishLevel = finishLevel;
WriteToLog(_startLevel, message);
}

/// <summary>
///
/// </summary>
public void Dispose()
{
TimeSpan elapsed = DateTime.Now - _start;

string msgStart = "Elapsed TIME:";
if (!string.IsNullOrWhiteSpace(_label))
{
msgStart = String.Format("Elapsed TIME for {0}:", _label);
}

string message = ConstructMessage(msgStart, elapsed);
WriteToLog(_finishLevel, message);
}

#region Private methods
private void WriteToLog(LogLevel level, string message)
{
// unfortunately, Common.Logging doesn't appear to provide a method that allows passing the level
switch (level)
{
case LogLevel.Debug:
_log.Debug(message);
break;
case LogLevel.Info:
_log.Info(message);
break;
case LogLevel.Trace:
_log.Trace(message);
break;
default:
_log.Warn("Unsupported LogLevel specified. Supported levels are Info, Debug and Trace. Falling back to Trace.");
_log.Trace(message);
break;
}
}

private static string ConstructMessage(string text, TimeSpan ts)
{
return ConstructMessage(text, ts.ToString());
}

private string ConstructMessage(string text, DateTime dt)
{
string time = dt.ToString(TimeFormat);
return ConstructMessage(text, time);
}

private static string ConstructMessage(string text, string time)
{
return String.Format("{0} ({1})", text, time);
}

#endregion
}
}
1 change: 1 addition & 0 deletions CtcApi/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Common.Logging" version="2.1.2" targetFramework="net40" />
<package id="Mvc3Assemblies" version="0.1.0" targetFramework="net40" />
</packages>
Loading