|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using WebExpress.WebApp.WebControl; |
| 5 | +using WebExpress.WebApp.WebScope; |
| 6 | +using WebExpress.WebApp.WebSettingPage; |
| 7 | +using WebExpress.WebCore.Internationalization; |
| 8 | +using WebExpress.WebCore.WebAttribute; |
| 9 | +using WebExpress.WebCore.WebLog; |
| 10 | +using WebExpress.WebCore.WebPage; |
| 11 | +using WebExpress.WebCore.WebSettingPage; |
| 12 | +using WebExpress.WebUI.Internationalization; |
| 13 | +using WebExpress.WebUI.WebControl; |
| 14 | +using WebExpress.WebUI.WebIcon; |
| 15 | + |
| 16 | +namespace WebExpress.WebApp.WWW.Setting.System |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Logging settings page. |
| 20 | + /// </summary> |
| 21 | + [WebIcon<IconFileMedicalAlt>] |
| 22 | + [Title("webexpress.webapp:setting.titel.log.label")] |
| 23 | + [SettingCategory<SettingCategoryGeneral>()] |
| 24 | + [SettingGroup<SettingGroupGeneral>()] |
| 25 | + [SettingSection(SettingSection.Secondary)] |
| 26 | + [Scope<IScopeAdmin>] |
| 27 | + public sealed class Log : ISettingPage<VisualTreeWebAppSetting>, IScopeAdmin |
| 28 | + { |
| 29 | + private readonly ILogManager _logManager; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the class. |
| 33 | + /// </summary> |
| 34 | + /// <param name="logManager">The log manager.</param> |
| 35 | + public Log(ILogManager logManager) |
| 36 | + { |
| 37 | + _logManager = logManager; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Processing of the resource. |
| 42 | + /// </summary> |
| 43 | + /// <param name="renderContext">The context for rendering the page.</param> |
| 44 | + /// <param name="visualTree">The visual tree of the web application.</param> |
| 45 | + public void Process(IRenderContext renderContext, VisualTreeWebAppSetting visualTree) |
| 46 | + { |
| 47 | + var downloadUri = renderContext.Request.Uri.Concat("download"); |
| 48 | + var log = _logManager?.DefaultLog.Filename; |
| 49 | + var file = new FileInfo(log); |
| 50 | + var fileSize = string.Format |
| 51 | + ( |
| 52 | + new FileSizeFormatProvider() |
| 53 | + { |
| 54 | + Culture = renderContext?.Request?.Culture |
| 55 | + }, |
| 56 | + "{0:fs}", |
| 57 | + file.Exists ? file.Length : 0 |
| 58 | + ); |
| 59 | + |
| 60 | + var deleteForm = new ControlModalFormConfirmDelete("delte_log") |
| 61 | + { |
| 62 | + Header = I18N.Translate |
| 63 | + ( |
| 64 | + renderContext, |
| 65 | + "webexpress.webapp:setting.logfile.delete.header" |
| 66 | + ), |
| 67 | + Content = new ControlFormItemStaticText() |
| 68 | + { |
| 69 | + Text = I18N.Translate |
| 70 | + ( |
| 71 | + renderContext, |
| 72 | + "webexpress.webapp:setting.logfile.delete.description" |
| 73 | + ) |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + deleteForm.Confirm += (s, e) => |
| 78 | + { |
| 79 | + File.Delete(log); |
| 80 | + }; |
| 81 | + |
| 82 | + var switchOnForm = new ControlModalFormConfirm("swichon_log") |
| 83 | + { |
| 84 | + // Header = this.I18N("webexpress.webapp", "setting.logfile.switchon.header"), |
| 85 | + // Content = new ControlFormItemStaticText() { Text = this.I18N("webexpress.webapp", "setting.logfile.switchon.description") }, |
| 86 | + // SubmitButtonIcon = new PropertyIcon(TypeIcon.PowerOff), |
| 87 | + // SubmitButtonColor = new PropertyColorButton(TypeColorButton.Success), |
| 88 | + // SubmitButtonLabel = this.I18N("webexpress.webapp", "setting.logfile.switchon.label") |
| 89 | + }; |
| 90 | + |
| 91 | + switchOnForm.Confirm += (s, e) => |
| 92 | + { |
| 93 | + // context.PluginContext.Host.Log.LogMode = LogMode.Override; |
| 94 | + // context.PluginContext.Host.Log.Info(this.I18N("webexpress.webapp", "setting.logfile.switchon.success")); |
| 95 | + }; |
| 96 | + |
| 97 | + var info = new ControlTable() |
| 98 | + { |
| 99 | + Striped = TypeStripedTable.Row |
| 100 | + } |
| 101 | + .AddColumn("") |
| 102 | + .AddColumn("") |
| 103 | + .AddColumn("") |
| 104 | + .AddRow |
| 105 | + ( |
| 106 | + new ControlTableCell() |
| 107 | + { |
| 108 | + Text = I18N.Translate |
| 109 | + ( |
| 110 | + renderContext, "webexpress.webapp:setting.logfile.path" |
| 111 | + ) |
| 112 | + }, |
| 113 | + new ControlTableCell() |
| 114 | + { |
| 115 | + Text = log, |
| 116 | + //Format = TypeFormatText.Code |
| 117 | + }, |
| 118 | + downloadUri != null && file.Exists |
| 119 | + ? new ControlTableCellPanel() |
| 120 | + .Add(new ControlButtonLink() |
| 121 | + { |
| 122 | + Text = I18N.Translate |
| 123 | + ( |
| 124 | + renderContext, |
| 125 | + "webexpress.webapp:setting.logfile.download" |
| 126 | + ), |
| 127 | + Icon = new IconDownload(), |
| 128 | + BackgroundColor = new PropertyColorButton(TypeColorButton.Primary), |
| 129 | + Uri = downloadUri |
| 130 | + }) |
| 131 | + : new ControlTableCell() |
| 132 | + ) |
| 133 | + .AddRow |
| 134 | + ( |
| 135 | + new ControlTableCell() |
| 136 | + { |
| 137 | + Text = I18N.Translate |
| 138 | + ( |
| 139 | + renderContext, |
| 140 | + "webexpress.webapp:setting.logfile.size" |
| 141 | + ) |
| 142 | + }, |
| 143 | + new ControlTableCell() |
| 144 | + { |
| 145 | + Text = file.Exists ? fileSize : "n.a." |
| 146 | + //Format = TypeFormatText.Code |
| 147 | + }, |
| 148 | + file.Exists |
| 149 | + ? new ControlTableCellPanel() |
| 150 | + .Add(new ControlButton() |
| 151 | + { |
| 152 | + Text = I18N.Translate |
| 153 | + ( |
| 154 | + renderContext, |
| 155 | + "webexpress.webapp:setting.logfile.delete.label" |
| 156 | + ), |
| 157 | + //Modal = new PropertyModal(TypeModal.Modal, deleteForm), |
| 158 | + Icon = new IconTrashAlt(), |
| 159 | + BackgroundColor = new PropertyColorButton(TypeColorButton.Danger) |
| 160 | + }) |
| 161 | + : new ControlTableCell() |
| 162 | + ) |
| 163 | + .AddRow |
| 164 | + ( |
| 165 | + new ControlTableCell() |
| 166 | + { |
| 167 | + Text = I18N.Translate |
| 168 | + ( |
| 169 | + renderContext, |
| 170 | + "webexpress.webapp:setting.logfile.modus" |
| 171 | + ) |
| 172 | + }, |
| 173 | + new ControlTableCell() |
| 174 | + { |
| 175 | + Text = _logManager?.DefaultLog.LogMode.ToString() |
| 176 | + //Format = TypeFormatText.Code |
| 177 | + }, |
| 178 | + _logManager?.DefaultLog.LogMode == LogMode.Off |
| 179 | + ? new ControlTableCellPanel() |
| 180 | + .Add(new ControlButton() |
| 181 | + { |
| 182 | + Text = I18N.Translate |
| 183 | + ( |
| 184 | + renderContext, |
| 185 | + "webexpress.webapp:setting.logfile.switchon.label" |
| 186 | + ), |
| 187 | + //Modal = new PropertyModal(TypeModal.Modal, switchOnForm), |
| 188 | + Icon = new IconPowerOff(), |
| 189 | + BackgroundColor = new PropertyColorButton(TypeColorButton.Success) |
| 190 | + }) |
| 191 | + : new ControlTableCell() |
| 192 | + ); |
| 193 | + |
| 194 | + visualTree.Content.MainPanel |
| 195 | + .AddPrimary(new ControlText() |
| 196 | + { |
| 197 | + Text = I18N.Translate |
| 198 | + ( |
| 199 | + renderContext, |
| 200 | + "webexpress.webapp:setting.logfile.label" |
| 201 | + ), |
| 202 | + TextColor = new PropertyColorText(TypeColorText.Info), |
| 203 | + Margin = new PropertySpacingMargin(PropertySpacing.Space.Two) |
| 204 | + }) |
| 205 | + .AddPrimary(info); |
| 206 | + |
| 207 | + if (file.Exists) |
| 208 | + { |
| 209 | + var content = File.ReadLines(log).TakeLast(100); |
| 210 | + |
| 211 | + visualTree.Content.MainPanel |
| 212 | + .AddPrimary(new ControlText() |
| 213 | + { |
| 214 | + Text = I18N.Translate |
| 215 | + ( |
| 216 | + renderContext, |
| 217 | + "webexpress.webapp:setting.logfile.extract" |
| 218 | + ), |
| 219 | + Format = TypeFormatText.H3 |
| 220 | + }) |
| 221 | + .AddPrimary(new ControlText() |
| 222 | + { |
| 223 | + Text = string.Join("<br/>", content.Reverse()), |
| 224 | + Format = TypeFormatText.Code |
| 225 | + }); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | +} |
| 230 | + |
0 commit comments