jellyfin-server/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs

585 lines
23 KiB
C#
Raw Normal View History

2014-08-10 22:13:17 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Events;
using Microsoft.Extensions.Logging;
2014-08-10 22:13:17 +00:00
using MediaBrowser.Model.Tasks;
using MediaBrowser.Model.Updates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2016-10-24 02:45:23 +00:00
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Extensions;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Dto;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Authentication;
2014-08-10 22:13:17 +00:00
2016-11-03 07:14:14 +00:00
namespace Emby.Server.Implementations.Activity
2014-08-10 22:13:17 +00:00
{
public class ActivityLogEntryPoint : IServerEntryPoint
{
private readonly IInstallationManager _installationManager;
//private readonly ILogger _logger;
2014-08-10 22:13:17 +00:00
private readonly ISessionManager _sessionManager;
private readonly ITaskManager _taskManager;
private readonly IActivityManager _activityManager;
private readonly ILocalizationManager _localization;
private readonly ILibraryManager _libraryManager;
private readonly ISubtitleManager _subManager;
private readonly IUserManager _userManager;
private readonly IServerConfigurationManager _config;
private readonly IServerApplicationHost _appHost;
2018-09-12 17:26:21 +00:00
private readonly IDeviceManager _deviceManager;
2014-08-10 22:13:17 +00:00
2018-09-12 17:26:21 +00:00
public ActivityLogEntryPoint(ISessionManager sessionManager, IDeviceManager deviceManager, ITaskManager taskManager, IActivityManager activityManager, ILocalizationManager localization, IInstallationManager installationManager, ILibraryManager libraryManager, ISubtitleManager subManager, IUserManager userManager, IServerConfigurationManager config, IServerApplicationHost appHost)
2014-08-10 22:13:17 +00:00
{
_sessionManager = sessionManager;
_taskManager = taskManager;
_activityManager = activityManager;
_localization = localization;
_installationManager = installationManager;
_libraryManager = libraryManager;
_subManager = subManager;
_userManager = userManager;
_config = config;
_appHost = appHost;
2018-09-12 17:26:21 +00:00
_deviceManager = deviceManager;
2014-08-10 22:13:17 +00:00
}
public void Run()
{
2018-09-12 17:26:21 +00:00
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
2014-08-10 22:13:17 +00:00
2018-09-12 17:26:21 +00:00
_installationManager.PluginInstalled += _installationManager_PluginInstalled;
_installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
_installationManager.PluginUpdated += _installationManager_PluginUpdated;
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
2014-08-10 22:13:17 +00:00
_sessionManager.SessionStarted += _sessionManager_SessionStarted;
_sessionManager.AuthenticationFailed += _sessionManager_AuthenticationFailed;
_sessionManager.AuthenticationSucceeded += _sessionManager_AuthenticationSucceeded;
_sessionManager.SessionEnded += _sessionManager_SessionEnded;
_sessionManager.PlaybackStart += _sessionManager_PlaybackStart;
_sessionManager.PlaybackStopped += _sessionManager_PlaybackStopped;
2014-08-14 13:24:30 +00:00
//_subManager.SubtitlesDownloaded += _subManager_SubtitlesDownloaded;
2014-08-10 22:13:17 +00:00
_subManager.SubtitleDownloadFailure += _subManager_SubtitleDownloadFailure;
_userManager.UserCreated += _userManager_UserCreated;
_userManager.UserPasswordChanged += _userManager_UserPasswordChanged;
_userManager.UserDeleted += _userManager_UserDeleted;
2018-09-12 17:26:21 +00:00
_userManager.UserPolicyUpdated += _userManager_UserPolicyUpdated;
2015-03-02 05:16:29 +00:00
_userManager.UserLockedOut += _userManager_UserLockedOut;
2014-08-10 22:13:17 +00:00
2014-08-14 13:24:30 +00:00
//_config.ConfigurationUpdated += _config_ConfigurationUpdated;
//_config.NamedConfigurationUpdated += _config_NamedConfigurationUpdated;
2014-08-10 22:13:17 +00:00
2018-09-12 17:26:21 +00:00
_deviceManager.CameraImageUploaded += _deviceManager_CameraImageUploaded;
2014-08-10 22:13:17 +00:00
_appHost.ApplicationUpdated += _appHost_ApplicationUpdated;
}
2018-09-12 17:26:21 +00:00
void _deviceManager_CameraImageUploaded(object sender, GenericEventArgs<CameraImageUploadInfo> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("CameraImageUploadedFrom"), e.Argument.Device.Name),
Type = NotificationType.CameraImageUploaded.ToString()
});
}
2015-03-02 05:16:29 +00:00
void _userManager_UserLockedOut(object sender, GenericEventArgs<User> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("UserLockedOutWithName"), e.Argument.Name),
2018-09-12 17:26:21 +00:00
Type = NotificationType.UserLockedOut.ToString(),
UserId = e.Argument.Id
2015-03-02 05:16:29 +00:00
});
}
2014-08-10 22:13:17 +00:00
void _subManager_SubtitleDownloadFailure(object sender, SubtitleDownloadFailureEventArgs e)
{
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("SubtitleDownloadFailureFromForItem"), e.Provider, Notifications.Notifications.GetItemName(e.Item)),
2014-08-10 22:13:17 +00:00
Type = "SubtitleDownloadFailure",
ItemId = e.Item.Id.ToString("N"),
2018-09-12 17:26:21 +00:00
ShortOverview = e.Exception.Message
2014-08-10 22:13:17 +00:00
});
}
void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e)
{
var item = e.MediaInfo;
if (item == null)
{
//_logger.LogWarning("PlaybackStopped reported with null media info.");
2014-08-10 22:13:17 +00:00
return;
}
2017-06-09 19:26:54 +00:00
if (e.Item != null && e.Item.IsThemeMedia)
2014-08-26 02:30:52 +00:00
{
// Don't report theme song or local trailer playback
return;
}
2014-08-26 02:30:52 +00:00
2014-08-10 22:13:17 +00:00
if (e.Users.Count == 0)
{
return;
}
2014-10-02 00:28:16 +00:00
var user = e.Users.First();
2014-08-10 22:13:17 +00:00
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("UserStoppedPlayingItemWithValues"), user.Name, GetItemName(item), e.DeviceName),
Type = GetPlaybackStoppedNotificationType(item.MediaType),
UserId = user.Id
2014-08-10 22:13:17 +00:00
});
}
void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
var item = e.MediaInfo;
if (item == null)
{
//_logger.LogWarning("PlaybackStart reported with null media info.");
2014-08-10 22:13:17 +00:00
return;
}
2017-06-09 19:26:54 +00:00
if (e.Item != null && e.Item.IsThemeMedia)
2014-08-26 02:30:52 +00:00
{
// Don't report theme song or local trailer playback
return;
}
2014-08-10 22:13:17 +00:00
if (e.Users.Count == 0)
{
return;
}
2014-10-02 00:28:16 +00:00
var user = e.Users.First();
2014-08-10 22:13:17 +00:00
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("UserStartedPlayingItemWithValues"), user.Name, GetItemName(item), e.DeviceName),
Type = GetPlaybackNotificationType(item.MediaType),
UserId = user.Id
2014-08-10 22:13:17 +00:00
});
}
2018-09-12 17:26:21 +00:00
private static string GetItemName(BaseItemDto item)
{
var name = item.Name;
if (!string.IsNullOrEmpty(item.SeriesName))
{
name = item.SeriesName + " - " + name;
}
if (item.Artists != null && item.Artists.Length > 0)
{
name = item.Artists[0] + " - " + name;
}
return name;
}
private string GetPlaybackNotificationType(string mediaType)
{
if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.AudioPlayback.ToString();
}
if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.GamePlayback.ToString();
}
if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.VideoPlayback.ToString();
}
return null;
}
private string GetPlaybackStoppedNotificationType(string mediaType)
{
if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.AudioPlaybackStopped.ToString();
}
if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.GamePlaybackStopped.ToString();
}
if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.VideoPlaybackStopped.ToString();
}
return null;
}
2014-08-10 22:13:17 +00:00
void _sessionManager_SessionEnded(object sender, SessionEventArgs e)
{
string name;
var session = e.SessionInfo;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(session.UserName))
2014-08-10 22:13:17 +00:00
{
name = string.Format(_localization.GetLocalizedString("DeviceOfflineWithName"), session.DeviceName);
2014-08-14 13:24:30 +00:00
// Causing too much spam for now
return;
2014-08-10 22:13:17 +00:00
}
else
{
name = string.Format(_localization.GetLocalizedString("UserOfflineFromDevice"), session.UserName, session.DeviceName);
}
CreateLogEntry(new ActivityLogEntry
{
Name = name,
Type = "SessionEnded",
2014-10-02 00:28:16 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("LabelIpAddressValue"), session.RemoteEndPoint),
2018-09-12 17:26:21 +00:00
UserId = session.UserId
2014-08-10 22:13:17 +00:00
});
}
2018-09-12 17:26:21 +00:00
void _sessionManager_AuthenticationSucceeded(object sender, GenericEventArgs<AuthenticationResult> e)
2014-08-10 22:13:17 +00:00
{
2018-09-12 17:26:21 +00:00
var user = e.Argument.User;
2014-08-10 22:13:17 +00:00
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("AuthenticationSucceededWithUserName"), user.Name),
2014-08-11 14:15:53 +00:00
Type = "AuthenticationSucceeded",
2018-09-12 17:26:21 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("LabelIpAddressValue"), e.Argument.SessionInfo.RemoteEndPoint),
UserId = user.Id
2014-08-10 22:13:17 +00:00
});
}
void _sessionManager_AuthenticationFailed(object sender, GenericEventArgs<AuthenticationRequest> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("FailedLoginAttemptWithUserName"), e.Argument.Username),
2014-08-11 14:15:53 +00:00
Type = "AuthenticationFailed",
2014-08-11 23:41:11 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("LabelIpAddressValue"), e.Argument.RemoteEndPoint),
Severity = LogLevel.Error
2014-08-10 22:13:17 +00:00
});
}
void _appHost_ApplicationUpdated(object sender, GenericEventArgs<PackageVersionInfo> e)
{
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("MessageApplicationUpdatedTo"), e.Argument.versionStr),
Type = NotificationType.ApplicationUpdateInstalled.ToString(),
2014-08-10 22:13:17 +00:00
Overview = e.Argument.description
});
}
void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("MessageNamedServerConfigurationUpdatedWithValue"), e.Key),
Type = "NamedConfigurationUpdated"
});
}
void _config_ConfigurationUpdated(object sender, EventArgs e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = _localization.GetLocalizedString("MessageServerConfigurationUpdated"),
Type = "ServerConfigurationUpdated"
});
}
2018-09-12 17:26:21 +00:00
void _userManager_UserPolicyUpdated(object sender, GenericEventArgs<User> e)
2014-08-10 22:13:17 +00:00
{
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("UserPolicyUpdatedWithName"), e.Argument.Name),
Type = "UserPolicyUpdated",
UserId = e.Argument.Id
2014-08-10 22:13:17 +00:00
});
}
void _userManager_UserDeleted(object sender, GenericEventArgs<User> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("UserDeletedWithName"), e.Argument.Name),
Type = "UserDeleted"
});
}
void _userManager_UserPasswordChanged(object sender, GenericEventArgs<User> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("UserPasswordChangedWithName"), e.Argument.Name),
2014-10-02 00:28:16 +00:00
Type = "UserPasswordChanged",
2018-09-12 17:26:21 +00:00
UserId = e.Argument.Id
2014-08-10 22:13:17 +00:00
});
}
void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("UserCreatedWithName"), e.Argument.Name),
2014-10-02 00:28:16 +00:00
Type = "UserCreated",
2018-09-12 17:26:21 +00:00
UserId = e.Argument.Id
2014-08-10 22:13:17 +00:00
});
}
void _subManager_SubtitlesDownloaded(object sender, SubtitleDownloadEventArgs e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("SubtitlesDownloadedForItem"), Notifications.Notifications.GetItemName(e.Item)),
Type = "SubtitlesDownloaded",
ItemId = e.Item.Id.ToString("N"),
ShortOverview = string.Format(_localization.GetLocalizedString("ProviderValue"), e.Provider)
});
}
void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
{
string name;
var session = e.SessionInfo;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(session.UserName))
2014-08-10 22:13:17 +00:00
{
name = string.Format(_localization.GetLocalizedString("DeviceOnlineWithName"), session.DeviceName);
2014-08-14 13:24:30 +00:00
// Causing too much spam for now
return;
2014-08-10 22:13:17 +00:00
}
else
{
name = string.Format(_localization.GetLocalizedString("UserOnlineFromDevice"), session.UserName, session.DeviceName);
}
CreateLogEntry(new ActivityLogEntry
{
Name = name,
Type = "SessionStarted",
2014-10-02 00:28:16 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("LabelIpAddressValue"), session.RemoteEndPoint),
2018-09-12 17:26:21 +00:00
UserId = session.UserId
2014-08-10 22:13:17 +00:00
});
}
void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("PluginUpdatedWithName"), e.Argument.Item1.Name),
2018-09-12 17:26:21 +00:00
Type = NotificationType.PluginUpdateInstalled.ToString(),
2014-08-10 22:13:17 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("VersionNumber"), e.Argument.Item2.versionStr),
Overview = e.Argument.Item2.description
});
}
void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("PluginUninstalledWithName"), e.Argument.Name),
2018-09-12 17:26:21 +00:00
Type = NotificationType.PluginUninstalled.ToString()
2014-08-10 22:13:17 +00:00
});
}
void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e)
{
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("PluginInstalledWithName"), e.Argument.name),
2018-09-12 17:26:21 +00:00
Type = NotificationType.PluginInstalled.ToString(),
2014-08-10 22:13:17 +00:00
ShortOverview = string.Format(_localization.GetLocalizedString("VersionNumber"), e.Argument.versionStr)
});
}
2018-09-12 17:26:21 +00:00
void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
2014-08-10 22:13:17 +00:00
{
2018-09-12 17:26:21 +00:00
var installationInfo = e.InstallationInfo;
2014-08-10 22:13:17 +00:00
CreateLogEntry(new ActivityLogEntry
{
2018-09-12 17:26:21 +00:00
Name = string.Format(_localization.GetLocalizedString("NameInstallFailed"), installationInfo.Name),
Type = NotificationType.InstallationFailed.ToString(),
ShortOverview = string.Format(_localization.GetLocalizedString("VersionNumber"), installationInfo.Version),
Overview = e.Exception.Message
2014-08-10 22:13:17 +00:00
});
}
void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
{
var result = e.Result;
var task = e.Task;
var activityTask = task.ScheduledTask as IConfigurableScheduledTask;
if (activityTask != null && !activityTask.IsLogged)
2014-08-10 22:13:17 +00:00
{
return;
}
2014-08-10 22:13:17 +00:00
var time = result.EndTimeUtc - result.StartTimeUtc;
var runningTime = string.Format(_localization.GetLocalizedString("LabelRunningTimeValue"), ToUserFriendlyString(time));
2014-10-24 04:54:35 +00:00
if (result.Status == TaskCompletionStatus.Failed)
2014-08-10 22:13:17 +00:00
{
var vals = new List<string>();
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(e.Result.ErrorMessage))
2014-08-10 22:13:17 +00:00
{
vals.Add(e.Result.ErrorMessage);
}
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(e.Result.LongErrorMessage))
2014-08-10 22:13:17 +00:00
{
vals.Add(e.Result.LongErrorMessage);
}
CreateLogEntry(new ActivityLogEntry
{
Name = string.Format(_localization.GetLocalizedString("ScheduledTaskFailedWithName"), task.Name),
2018-09-12 17:26:21 +00:00
Type = NotificationType.TaskFailed.ToString(),
2018-12-28 15:48:26 +00:00
Overview = string.Join(Environment.NewLine, vals.ToArray()),
2014-08-11 23:41:11 +00:00
ShortOverview = runningTime,
Severity = LogLevel.Error
2014-08-10 22:13:17 +00:00
});
}
}
private void CreateLogEntry(ActivityLogEntry entry)
2014-08-10 22:13:17 +00:00
{
try
{
_activityManager.Create(entry);
2014-08-10 22:13:17 +00:00
}
catch
{
// Logged at lower levels
}
}
public void Dispose()
{
_taskManager.TaskCompleted -= _taskManager_TaskCompleted;
_installationManager.PluginInstalled -= _installationManager_PluginInstalled;
_installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
_installationManager.PluginUpdated -= _installationManager_PluginUpdated;
2018-09-12 17:26:21 +00:00
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
2014-08-10 22:13:17 +00:00
_sessionManager.SessionStarted -= _sessionManager_SessionStarted;
_sessionManager.AuthenticationFailed -= _sessionManager_AuthenticationFailed;
_sessionManager.AuthenticationSucceeded -= _sessionManager_AuthenticationSucceeded;
_sessionManager.SessionEnded -= _sessionManager_SessionEnded;
_sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
_sessionManager.PlaybackStopped -= _sessionManager_PlaybackStopped;
_subManager.SubtitlesDownloaded -= _subManager_SubtitlesDownloaded;
_subManager.SubtitleDownloadFailure -= _subManager_SubtitleDownloadFailure;
_userManager.UserCreated -= _userManager_UserCreated;
_userManager.UserPasswordChanged -= _userManager_UserPasswordChanged;
_userManager.UserDeleted -= _userManager_UserDeleted;
2018-09-12 17:26:21 +00:00
_userManager.UserPolicyUpdated -= _userManager_UserPolicyUpdated;
2015-03-02 05:16:29 +00:00
_userManager.UserLockedOut -= _userManager_UserLockedOut;
2014-08-10 22:13:17 +00:00
_config.ConfigurationUpdated -= _config_ConfigurationUpdated;
_config.NamedConfigurationUpdated -= _config_NamedConfigurationUpdated;
2018-09-12 17:26:21 +00:00
_deviceManager.CameraImageUploaded -= _deviceManager_CameraImageUploaded;
2014-08-10 22:13:17 +00:00
_appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
}
/// <summary>
/// Constructs a user-friendly string for this TimeSpan instance.
/// </summary>
public static string ToUserFriendlyString(TimeSpan span)
{
const int DaysInYear = 365;
const int DaysInMonth = 30;
// Get each non-zero value from TimeSpan component
List<string> values = new List<string>();
// Number of years
int days = span.Days;
if (days >= DaysInYear)
{
2016-03-27 21:11:27 +00:00
int years = days / DaysInYear;
2014-08-10 22:13:17 +00:00
values.Add(CreateValueString(years, "year"));
2016-03-27 21:11:27 +00:00
days = days % DaysInYear;
2014-08-10 22:13:17 +00:00
}
// Number of months
if (days >= DaysInMonth)
{
2016-03-27 21:11:27 +00:00
int months = days / DaysInMonth;
2014-08-10 22:13:17 +00:00
values.Add(CreateValueString(months, "month"));
2016-03-27 21:11:27 +00:00
days = days % DaysInMonth;
2014-08-10 22:13:17 +00:00
}
// Number of days
if (days >= 1)
values.Add(CreateValueString(days, "day"));
// Number of hours
if (span.Hours >= 1)
values.Add(CreateValueString(span.Hours, "hour"));
// Number of minutes
if (span.Minutes >= 1)
values.Add(CreateValueString(span.Minutes, "minute"));
// Number of seconds (include when 0 if no other components included)
if (span.Seconds >= 1 || values.Count == 0)
values.Add(CreateValueString(span.Seconds, "second"));
// Combine values into string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < values.Count; i++)
{
if (builder.Length > 0)
2016-03-27 21:11:27 +00:00
builder.Append(i == values.Count - 1 ? " and " : ", ");
2014-08-10 22:13:17 +00:00
builder.Append(values[i]);
}
// Return result
return builder.ToString();
}
/// <summary>
/// Constructs a string description of a time-span value.
/// </summary>
/// <param name="value">The value of this item</param>
/// <param name="description">The name of this item (singular form)</param>
private static string CreateValueString(int value, string description)
{
return String.Format("{0:#,##0} {1}",
2016-03-27 21:11:27 +00:00
value, value == 1 ? description : String.Format("{0}s", description));
2014-08-10 22:13:17 +00:00
}
}
}